0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

So, data never performs actions at exact same time for different clients. But one after another instead. Right? @leroydev
If you'd query a database or read from filesystem within a callback, it will wait with the execution of the callback function for that operation untill ready. In the meantime, the event loop will take on other events, which could be a second execution of the same fallback.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.