0

I'm new to socket io and I'm quite confused because I receive duplicate messages from the server. I'm using flask-socketio on the server side.

Here's the server side code:

class StdOutListener1(StreamListener):
    def on_data(self, data):
        data = json.loads(data)
        socketio.emit('streamTweets1',{"data": data},namespace='/tweet')

@socketio.on("connect", namespace="/tweet")
def connectServer():
    print("Client connected")
    socketio.emit("connected", namespace="/tweet")


@socketio.on('startTweets1', namespace='/tweet')
def tweetStreaming1():
    stream_1 = Stream(auth_1, l1)
    stream_1.filter(track=search_list_1)

stream_1.filter(track=search_list_1) will call the on_data function in StdOutListener1 class.

my client side code is:

    socket = io.connect("http://" + document.domain + ":" + location.port + "/tweet");
    // listen to the event 'connected'
    socket.on("connected", function(data) {
      console.log("listening connected...");
      socket.emit("startTweets1");
    });

socket.on("streamTweets1", function(data){ console.log(data) });

every time after I refresh my client side page, I will get duplicate results.

Also, I'm not sure how to stop or disconnect. I tried to use control + c to for the server to stop, but it seems it will continue running. how do I correctly stop the server and disconnect?

al3xtouch
  • 491
  • 4
  • 19
  • Not familiar with the `StreamListener` class. How long does it take for the `filter()` method to do its job and return? – Miguel Grinberg Jul 26 '16 at 21:25
  • @Miguel it's pretty fast actually. When I get duplicate results, they come simultaneously actually. I don't get duplicate results all the time, if I start the server fresh and access the web page on my client side, it's fine. But when I refresh the page, duplicate results will appear, I don't know why, I got to kill the server process and refresh the page several time to get back to the normal result. Is it because every time a client connects to the server, tweetStreaming1() get triggered? – al3xtouch Jul 28 '16 at 00:11
  • @Miguel here's is the definition of the `StreamListerner` https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py – al3xtouch Jul 28 '16 at 00:13
  • When I said StreamListener that was a mistake. The filter method that you are calling is on class `Stream`. That's the one I need info on. Basically what does it do and how long until it returns. – Miguel Grinberg Jul 28 '16 at 01:24
  • @Miguel The stream class takes an auth and a listener, the listener is the StreamListener. The code for the Stream class is also listed here:https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py – al3xtouch Jul 28 '16 at 07:30
  • Sorry, now I found it. Well, the problem is clear. Each time you call filter a background thread starts. In the way you have coded your application each refresh will start another background thread, you are never stopping those. – Miguel Grinberg Jul 28 '16 at 17:36
  • @Miguel could you point to the reference of how to close the background thread (or even some examples)? I'm really new to socket.io and was just trying to make it "work" under the time constraint, but now I want to make it function. Also, I never disconnect from the socket, is there a problem? – al3xtouch Jul 28 '16 at 17:56

0 Answers0