Socket.io load balancing with Nginx Zero to Hero
I started two instances in node js at server level. Problem Faced
-
Every time nginx is taking my request of socket to alternate instance
-
Socket connection is on first instace
-
One instance is returning 200 and other 400 for a single user due to Round Robin
Then we applied the ip_hash on nginx level
Problem Solved
-
Now i have a sticky connection with the instance
-
socket is working now
Problem Faced
-
Connections are on different instances
-
Not able to broadcast to other instance socket connections
-
One to One Message only work if both the socket are on same connection
Now we have Redis in the picture
we have to use a npm module socket.io-redis
Note. This required to install redis-server as well
npm install socket.io-redis --save
Add these line after io require and before io.connection
const redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
Problem Solved
- now all the instances are Synced io.broadcast is working fine