Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate message shown by socket io

I am using redis for notification and it works like this -

When user1 has a notification to share with his friends, I publish that message to all channels of each friend of the user. Using socket.io and node, message is pushed to each friend as subscription part is handled using node.

This works fine until connection is lost. At this time, on reconnect, I am finding instead of 1, 2 publishes are happening to friend's channels although only 1 activity happened like last case..

Is their any config using which duplicate publish can be avoided on reconnect ?

Also finding, on reconnect it is trying to connect using secondary transports as well .. Can this be issue ?

like image 206
fortm Avatar asked Aug 24 '26 01:08

fortm


1 Answers

I suppose that on each connection

io.sockets.on('connection', function(client){
//...
});

you subscribe to redis

redisClient.subscribe('activity:*');

There is no need to connect to redis each time. You should connect to redis just before socket connection.

like image 178
kotapaulta Avatar answered Aug 25 '26 15:08

kotapaulta