jdscardoso
jdscardoso

Reputation: 291

Nodejs JavaScript Events

Given the code above:

binaryServer = BinaryServer({port: 9001});

binaryServer.on('connection', function(client) {
  console.log("new connection");


  client.on('stream', function(stream, meta) {
    console.log('new stream');
    stream.on('data', function('data'){
    //actions

    stream.on('end', function() {
      //actions
    });
  });
});

I can say that client inherits the features of binaryServer. So if I make console.log(client.id) in the events of stream I can see, which client generate the given event. Now I want to know if every single event is exclusive of one client, in other words I want to know if data happens for every single client (that generates data) and no data event will be generated while the actions is happening.

Upvotes: 0

Views: 68

Answers (1)

leroydev
leroydev

Reputation: 2945

You're registering a listener to the "connection" event which can happen within binaryServer. When a "connection" event happens, the registered listener will receive an argument, which you choose to call client. client in this case is an object, and doesn't inherit features of binaryServer.

"data" happens for every client, but will have unique results for each clientsince you register an event listener for every client.

If two events are triggered after each other, the callback function of the first event will be called, and after that the second events callback function will be called. See the following example code:

var event = new Event('build');
var i = 0;

// Listen for the event.
document.addEventListener('build', function (e) {
    console.log(i++);
}, false);

// Dispatch the event.
document.dispatchEvent(event);
document.dispatchEvent(event);

JSFiddle (watch console)

Information about JavaScript inheritance

Information about JavaScript event loop

Upvotes: 1

Related Questions