Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS & Socket.IO: Emit a request event and get the response, when/where should I bind the listener?

I'm currently wondering about what could be the best programming practice in this case:

Let's say I've got client connected to my server. And this client is asking the server for authentication with an auth event and his username.

socket = io();
socket.emit('auth', "John");

In this simple case, server is responding with an auth_succeed event with the users's id.

io.on('connection', function(socket) {
    socket.on('auth', function(username) {
        socket.emit('auth_succeed', id);
    }
}

So my question is, when or where should I bind the listener for the auth_succeed event in the client ? There's two ways for me :

Before emitting, which ensures, I guess, that the response event will always be properly handled, but leads to some spaghetti code. Ex:

socket = io();
socket.on('auth_succeed', function(id){
    //Do some post-auth stuff here
}); 
socket.emit('auth', "John");

Or after emitting, which leads to cleaner code, but could possibly, I guess again, miss the event if sended quick enough. Ex:

socket = io();
socket.emit('auth', "John");
socket.on('auth_succeed', function(id){
    //Do some post-auth stuff here
}); 

What's your thoughts about that problem ?

like image 514
Maxime Piraux Avatar asked Dec 31 '14 11:12

Maxime Piraux


People also ask

What is NodeJS used for?

It is used for server-side programming, and primarily deployed for non-blocking, event-driven servers, such as traditional web sites and back-end API services, but was originally designed with real-time, push-based architectures in mind.

Is NodeJS frontend or backend?

Node. js is sometimes misunderstood by developers as a backend framework that is exclusively used to construct servers. This is not the case; Node. js can be used on the frontend as well as the backend.

Is NodeJS a programming language?

In a word: no. Node. js is not a programming language. Rather, it's a runtime environment that's used to run JavaScript outside the browser.

Is NodeJS better than Java?

Java uses the concept of multithreading with ease, whereas Node JS does not use the concept of multi-threading like Java does. For large scale projects that involved concurrency, Java is highly recommended, whereas Node JS does not handle the thread and Java, which is the weakest point of this framework.


1 Answers

Since the response from the emit should be asynchronous, and the client-side JS is synchronous in nature, the socket.on('auth_succeed' binding will happen before the callback from the auth event.


The following flow will happen on the client...

// EXECUTION SCOPE BEGINS
...
// this will send a message to the server
// the message and/or response will be sent asynchronously
socket.emit('auth', 'John'); 
// so your code will continue before you get anything from the server

//which means the following binding will happen before any response to the above emit
socket.on('auth_succeed', function(id){
  //... handle message from server ...
});
...
// EXECUTION SCOPE ENDS

Sometime after the enclosing scope/function is done executing the event of 'auth_succeed' be raised.


You may also want to consider breaking out your event handlers...

socket.on('auth_succeed', onAuthSucceed.bind(null, socket));
socket.emit('auth', 'john');

// ... elsewhere ...

function onAuthSucceed(socket, id) {
  // handle message from server
}

This will reduce the noise with your binding, and your signal event, regardless of if you choose to bind or emit first.

By having the function require anything it needs, and using binding for the event, the method in question can be in a separate file/module, and more easily tested in isolation.

like image 75
Tracker1 Avatar answered Oct 05 '22 12:10

Tracker1