Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io Best coding practice

I'm developing a Node.js application which uses Socket.io to handle the real time communication. My code is full of On and Emit functions. I use room feature as well. my app looks like this:

   var server = require('http').Server();
    var io = require('socket.io')(server);
    io.on('connection', function(socket){
      socket.on('event1', function(data){"lots of socket and non-socket codes" });
      socket.on('event2', function(data){"lots of socket and non-socket codes" });
      socket.on('event3', function(data){"lots of socket and non-socket codes" });
      socket.on('disconnect', function(){"Some other code"});
    });
    server.listen(portNum); 

it works fine but It is not my ideal solution. Firstly, in this approach everything is in one big file instead of smaller file with isolated functionality.

secondly, it is quite difficult to debug and maintain this app as it is quite messy when it comes to 1000+ lines of code.

Here is my question:

Is there a preferred/best practice on developing enterprise quality Socke.io applications? If yes, Is there any large opensource Socket.io application that demonstrait this approch or any article which help me to refactor my code in a better fassion?

like image 596
Raiss Avatar asked Jun 11 '14 00:06

Raiss


People also ask

Is Socket.IO difficult?

Socket.IO makes many things easier and provides fallbacks for unsupported clients, but has its own trade-offs. Scaling applications is perhaps the most difficult step in using sockets, and Socket. IO's implementation for non-WebSocket connections further complicates the process. Socket.

Which is better Socket.IO or WS?

Both WebSocket vs Socket.io are popular choices in the market; let us discuss some of the major Difference Between WebSocket vs Socket.io: It provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback.

Is Socket.IO efficient?

Using socket.io is broadcasting the same efficiency as sending a message to every client.


2 Answers

I think starting by putting every callback function in a different function that you put out of io.on('connection'), and maybe also put them in a different file (using module.exports), you will start having a clearer application.

Ok so i will write you one possibility that i use, i don't know if it's the best pattern of the universe for socket.io, but that's good i think.

In your main file (the file with io.onconnection), you can have something like this (you dont have to use namespace, it's just an example) :

var SocketEvent = require('./socketEvent');

io.of('/user').on('connection', function (socket) {

    SocketEvent.load_common_event(socket);
    SocketEvent.load_user_event(socket);

});

io.of('/operator').on('connection', function (socket) {

    SocketEvent.load_common_event(socket);
    SocketEvent.load_operator_event(socket);

});

And in the socketEvent.js that you load you can have this :

exports.load_common_event = function(socket){
    socket.on('disconnect', function(){"Some other code"});
};

exports.load_user_event = function(socket){
    socket.on('event1', function(data){"lots of socket and non-socket codes" });
    socket.on('event2', function(data){"lots of socket and non-socket codes" });
    socket.on('event3', function(data){"lots of socket and non-socket codes" });
};

exports.load_operator_event = function(socket){
    socket.on('event4', function(data){"lots of socket and non-socket codes" });
    socket.on('event5', function(data){"lots of socket and non-socket codes" });
    socket.on('event6', function(data){"lots of socket and non-socket codes" });
};

Let me know if you have any question

Add-on

If you want something like Socket.on('event' , myModule.doSomething);

you can do like this i guess in the module :

client :

var myModule = require('./socketModule');

io.on('connection', function (socket) {

    socket.on('event' , myModule.doSomething(/*some parameters (socket)*/));

});

server socketModule.js :

exports.doSomething = function(/*some parameters (socket)*/){
    /* Some processing around */
};
like image 137
Jujuleder Avatar answered Oct 06 '22 08:10

Jujuleder


For those who are wondering about pass paremeters, .bind could be an option

const events = require('./events.js')
io.of('/chat').on('connection', (socket) => {
  socket.on('print', events.print.bind({socket, io}))
})

event.js

const util = require('util')
function print(data) {
  console.log(util.inspect(this.socket).substr(0, 100))
  console.log(util.inspect(this.io).substr(0, 100))
}
module.exports = {
  print
}
like image 31
Alex Avatar answered Oct 06 '22 09:10

Alex