Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check socket is alive (connected) in socket.io with multiple nodes and socket.io-redis

I am using socket.io with multiple nodes, socket.io-redis and nginx. I follow this guide: http://socket.io/docs/using-multiple-nodes/

I am trying to do: At a function (server site), I want to query by socketid that this socket is connected or disconnect

I tried io.of('namespace').connected[socketid], it only work for current process ( it mean that it can check for current process only).

Anyone can help me? Thanks for advance.

like image 233
John Nguyen Avatar asked Apr 01 '15 12:04

John Nguyen


People also ask

How can I tell if Socket.IO is connected?

You can check the socket. connected property: var socket = io. connect(); console.

How many connection can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Does Socket.IO use Redis?

The socket. io-redis plugin uses the pub/sub client of the redis server to connect multiple socket.io instances.

How does Socket.IO Redis work?

How it works​ The Redis adapter relies on the Redis Pub/Sub mechanism. Every packet that is sent to multiple clients (e.g. io.to("room1"). emit() or socket.


2 Answers

How can I check socket is alive (connected) with socketid I tried namespace.connected[socketid], it only work for current process.

As you said, separate process means that the sockets are only registered on the process that they first connected to. You need to use socket.io-redis to connect all your nodes together, and what you can do is broadcast an event each time a client connects/disconnects, so that each node has an updated real-time list of all the clients.

like image 156
Tristan Foureur Avatar answered Nov 13 '22 09:11

Tristan Foureur


Check out here

as mentioned above you should use socket.io-redis to get it work on multiple nodes.

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
like image 43
vromanch Avatar answered Nov 13 '22 09:11

vromanch