How to stop tweets from getting duplicated using Socket.io?

1,242 views
Skip to first unread message

Aryak Sengupta

unread,
Apr 15, 2015, 8:12:04 AM4/15/15
to nod...@googlegroups.com
I am simply doing something like this:

var stream = T.stream('statuses/filter', { track: watchList })

  stream.on('tweet', function (tweet) {

    io.sockets.emit('stream',tweet.text);
    

  });
 });

I am using this for as the Twitter API client (Twit


Everything works fine and I am also able to see watch the stream of tweets for the track words which I provide in watchList. But if I refresh the page, a new connection is established again without terminating the old connection. For that reason, the tweets are getting duplicated.

 How do I stop this from happening?Or how can I check whether a connection is already established? 

Thank You,
Aryak

Aryak Sengupta

unread,
Apr 16, 2015, 2:42:50 PM4/16/15
to nod...@googlegroups.com
Please help.. anyone!!

Matthias Bleyl

unread,
Apr 17, 2015, 10:10:19 AM4/17/15
to nod...@googlegroups.com
1. Avoid refreshing webpage
2. For all socket.io clients: register IP address and port. For all new connections ("on connect") double check for duplicates.
Matthias

Aryak Sengupta

unread,
Apr 18, 2015, 12:15:06 PM4/18/15
to nod...@googlegroups.com

Thanks a lot Matthias for your response. Can you please elaborate on what exactly you mean by "double check for duplicates"

Thanks,
Aryak

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/ad455649-7c31-4f75-a779-5974a32dfdb7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Darshit Shah

unread,
Apr 19, 2015, 8:43:27 AM4/19/15
to nod...@googlegroups.com
Hey Aryak,
You haven't mentioned if watchList is global/common for each client or at the time of connection client is submitting its own watchList?

If your watchList is common between all clients then you need to write your sample code such that it executes only once. E.g.

var io = require('socket.io')(80);

var stream = T.stream('statuses/filter', { track: watchList })
stream.on('tweet', function (tweet { io.emit('stream',tweet.text);
});
});

io.on('connection', function(socket){
// nothing.
});

But if client is submitting its own watchList then approach is different. In that case instead of writing io.emit you should emit to only client. Below is sample code for that.

var io = require('socket.io')(80);
io.on('connection', function(socket){
socket.on('watchList', function(watchList){
var stream = T.stream('statuses/filter', { track: watchList });
stream.on('tweet', function (tweet) { socket.emit('stream',tweet.text);
});
});
});
});

zladuric

unread,
Apr 19, 2015, 8:43:40 AM4/19/15
to nod...@googlegroups.com
Well check for duplicates by the IP:port:client key or something similar.

Also I'm pretty sure you can also listen for client.on('disconnect') on the server and remove that client on hard page refresh.

Matthias Bleyl

unread,
Apr 19, 2015, 8:43:58 AM4/19/15
to nod...@googlegroups.com

Aryak,

I thought I had a similar problem as you described in your first post (concerning REFRESHing html pages) which I solved in a way I described in my first answer.
However, double checking this I was not able to reproduce it anymore. May be I was wrong.

The only problem I still can reproduce is that  the socket.io client (html page) establishes a additional second connection (without terminating the old connection) when it tries to RECONNECT the existing connection. This happens e.g. in the case that the server is restarted - but not when the html page is just REFRESHED!

In such a situation I get two "active" connections for one and the same html client - which causes the problems you described.
However, this can be solved easily by correcting the client script:

Wrong version (produces doubled entries in case of a server restart):
     mySocket.on('connect', function()
    {
        console.log("connected")
        mySocket.on("message", function(data){console.log("update watchList with: "+data)});
    }); 

Correct version:
     mySocket.on('connect', function(){console.log("connected")}); 
     mySocket.on("message", function(data){console.log("update watchList with: "+data)});

Hope this helps.
Matthias

Aryak Sengupta

unread,
Apr 20, 2015, 11:10:01 AM4/20/15
to nod...@googlegroups.com
Thanks a lot everyone!!.

 Matthias , you were absolutely spot on. Thanks a lot. I resolved it using your approach.

Darshit Shah - Yes the client is submitting his own watchList. Thanks  

zladuric - Okay. Will try it out. Thanks



--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages