javascript - Is there a way to emit end event to all listeners at the same time with Node.js EventEmitter? -
as the docs says, emitter.emit(event[, arg1][, arg2][, ...])
synchronously calls each of listeners registered event, in order registered, passing supplied arguments each.
but simple example:
router.get('/messages', function(req, res, next) { var addmessagelistener = function(res){ messagebus.once('message', function(data){ res.json(data); }) } addmessagelistener(res); }); router.get('/push', function(req, res, next) { messagebus.emit('message', { awesome : 'event'}) res.status(200).end() }); when message event emitted , 2 request listening, 1 of them listen event , execute callback.
is there way emit event listeners @ same time?
edit:
i didnt need listeners execute @ exact same time, need execute of them
edit 2:
after more testing, when create more 1 listener, listening emitter (debugged messagebus.listenercount('message')) 20 seconds delay.
and when 1 listening , second 1 during delay, if call /push emit listening 1 , other 1 start listening.
since used once() function add listener, node call specified listener @ once, , after first call remove listener. if expecting listener called every time message event emitted, need use on() function instead of once().
Comments
Post a Comment