javascript - Socket io connects to multiple sockets on reconnect -
i have 1 application have implemented notification using using node js , socket io , redis.
there have configured publisher , subscriber in node js. using socket io can show messages on client realtime.
my design 1 publisher publishes many channels , every user listening own channel.
i publishes channel1 , channel2 , users listening channels only, getting respective messages 1 time.
till worked fine, smoothly.
but here problem starts. when internet goes offline tries reconnect , when again establishes connection, on every single publish starts subscribe twice , result of messeges showing twice.
my code on server subscriber side
io.sockets.on('connection', function (socket) { var channelname = "channel"+socket.handshake.query.ch; console.log("======================================================================="); console.log("connected client - ["+socket.id+"] ["+channelname+"]"); console.log("transport method - ["+io.transports[socket.id].name+"]"); console.log("======================================================================="); try{ var client = redis.createclient(); client.on("connect",function(){ client.subscribe(channelname)?console.log("channel subscribed {"+channelname+"}"):console.log("not subscribed {"+channelname+"}"); client.on("message", function(channel,message){ console.log("======================================================================="); console.log("got message ["+message+"] in ["+channel+"]"); socket.emit("notification"+channel, { msg: message }); console.log("======================================================================="); }); }); }catch(e){ console.log("redis server not running !!"); } socket.on('disconnect', function(){ console.log("disconnected client !!"); }); });
on client side code this
<script src="localhost:3000/socket.io/socket.io.js"></script> <script> // 1 client ch=1 , ch=2 var socket = io.connect("localhost:3000",{ query: "ch=1", "try multiple transports":false}); socket.on('connect', function(data){ $("div#status").html("<div class='connected'></div><span>connected</span>"); }); // 1 client event notificationchannel1 , client event notificationchannel2 socket.on('notificationchannel1',function(data){ $("div#notification-div").append(data.msg); }); socket.on('connecting', function(data){ $("div#status").html("<div class='connecting'></div><span>connecting</span>"); }); socket.on('disconnect', function(data){ $("div#status").html("<div class='disconnected'></div><span>disconnected</span>"); }); socket.on('reconnecting', function(data){ $("div#status").html("<div class='reconnecting'></div><span>reconnecting...</span>"); }); </script>
please suggest me things missing.
Comments
Post a Comment