How to check socket is alive (connected) in socket

2019-04-19 10:45发布

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.

2条回答
【Aperson】
2楼-- · 2019-04-19 11:23

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-19 11:30

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 }));
查看更多
登录 后发表回答