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?
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.
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.
Using socket.io is broadcasting the same efficiency as sending a message to every client.
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 */
};
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With