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?