0

I am trying to build a notification system with Flask-SocketIO. I want that the server should be able to send messages independently to any chosen client. I am trying to achieve this by keeping each client in a separate room (the default session ID room). But I am unable to see any messages on the client when the server tries to send any. Following are relevant snippets from my code:

Server Code:

# maintain a global session id list of connected clients
sidlist = []

@socketio.on('connect')
def handle_connect():
    '''
    Append the new sid to the global sid list
    '''
    sidlist.append(request.sid)


def messenger():
    '''
    Simple stupid test that runs in a separate thread trying
    to send messages every 10 seconds
    :return:
    '''
    for i in range(0,100):
        if len(sidlist) > 0:
            idx = i % len(sidlist)
            socketio.emit('server-message', 'Message sent at time: ' + str(i), room=sidlist[idx])
        sleep(10)

Client Code:

$(document).ready(function(){
        var socket = io.connect();
        socket.on('server-message', function(msg) {
            console.log('got a server message');
            $('#log').append('<p>Received: ' + msg + '</p>');
        });
});

I do see logs on the server side that an emit was attempted to a room specified by the session ID. But I don't see the message being received on the client. Am I missing something? Is SessionID not the default room for each connected client?

Uday Mittal
  • 73
  • 2
  • 8
  • Warning: the global variable _sidlist_ is not thread (neither process) safe. You may use an external tool for this kind of uses, such as redis or memcached. Take a look to that post: http://stackoverflow.com/a/32825482/1674908 – Hamlett May 28 '16 at 15:09
  • Wow! Thanks for the heads up @Hamlett! I'll shift that out to a data store. – Uday Mittal May 29 '16 at 01:59
  • Is there anything in the client side logs? See http://socket.io/docs/logging-and-debugging/ for instructions to enable logs in the browser. – Miguel Grinberg May 29 '16 at 19:27
  • @Miguel I turned on the client side logs and saw that there is just a polling GET request being sent at a fixed interval. That log looks like this: `engine.io-client:polling-xhr xhr open GET: http://localhost:5200/socket.io/?EIO=3&transport=polling&t=1464566423903-4 +1ms` Is there something wrong in the way that I am initialising the socketio object in Python? `app = Flask(__name__) socketio = SocketIO(app)` – Uday Mittal May 30 '16 at 00:02
  • @UdayMittal the console in your debugger should show a detailed log of all the socket.io activities. What I was hoping is that you would see something related to messages received from the server. – Miguel Grinberg May 30 '16 at 02:43
  • @Miguel, I didn't see any messages being received from the server in the console with the debugger. After a lot of search and random tries, I am finally able to send messages to the client by setting the async_mode argument while creating the socketIO object: `socketio = SocketIO(app, async_mode='threading')`. I don't understand why this is needed though or what it does. Maybe including this as a note in the documentation/examples will be helpful. Thanks! :) – Uday Mittal Jun 02 '16 at 03:37
  • Running in threading mode is not ideal. Sure, it works, but in this mode there is no WebSocket support, so performance and latency are both going to be bad. Does the example application in the Flask-SocketIO repository work for you? If that works, then there must be something different in your code that prevents the emits from getting through. – Miguel Grinberg Jun 02 '16 at 04:53
  • I see. Yes the example application works @Miguel. I can see server messages on the page. I didn't see this example before. I'll read through it carefully and compare to see what I am missing. Thank you! – Uday Mittal Jun 02 '16 at 06:26
  • @Miguel I figured I was doing the monkey_patch() after importing the time module all this while. This had been causing the problem of not being able to send messages from a different thread that used time.sleep() – Uday Mittal Jun 08 '16 at 11:35

0 Answers0