I am involved in a development project of a chat where we are using node.js, socket.io (rooms) and mongodb. We are at the stage of performance testing and we are very concerned if the system needs a load balance.
How can we develop if our project needs it? J'a researched on NGINX looks cool, but we are in doubt whether solves our problem as how the system will be a chat, we fear the servers are not ~talking~ with each other correctly ...
Where do we go if we need a load balancing?
"The Socket.IO server currently has some problems with scaling up to more than 10K simultaneous client connections when using multiple processes and the Redis store, and the client has some issues that can cause it to open multiple connections to the same server, or not know that its connection has been severed."
Here we can see that the HTTP benchmark peaks at about~950 requests per second while Socket.io serves about ~3900 requests per second.
Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.
It's pointless to load balance a single server. What load balancing does is decide which server to use based on some criteria (typically load). You could have a load balance installed, but it wouldn't be serving any purpose. Another major reason for multiple servers isn't load balancing, but redundancy.
To ensure that we can scale to multiple nodes but keep up interconnectivity between different clients and different servers, I use redis. It's actually very simple to use and set up.
What this does is creates a pub/sub system between your servers to keep track of your different socket clients.
var io = require('socket.io')(3000),
redis = require('redis'),
redisAdapter = require('socket.io-redis'),
port = 6379,
host = '127.0.0.1',
pub = redis.createClient(port, host),
sub = redis.createClient(port, host, {detect_buffers: true}),
server = http(),
socketServer = io(server, {adapter: redisAdapter({pubClient: pub, subClient: sub})});
read more here: socket.io-redis
As far as handling the different node servers, there are different approaches.
Among others...
Check out the NPM package mong.socket.io . It has the ability to save socket.io data to mongoDB like below;
{
"_id" : ObjectId("54b901332e2f73f5594c6267"),
"event" : "join",
"message" : {
"name" : "join",
"nodeId" : 426506139219,
"args" : "[\"URAiA6mO6VbCwquWKH0U\",\"/54b6821asdf66asdasd2f0f9cd2997413780273376\"]"
}}
Or you may use the redis adapter as mentioned there;
Socket.IO Using multiple nodes
Then just use the NGINX reverse proxy and all of the node processes should share Socket.IO events with each other.
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