Broadcast to all connected clients except sender with python flask socketio
You don't have to save users ids and manage individual emissions, you can specify a broadcast without the sender with emit('my_event', {data:my_data}, broadcast=True, include_self=False)
. In your case it would be something like this:
@socketio.on('send_message') def handle_source(json_data): text = json_data['message'].encode('ascii', 'ignore') emit('echo', {'echo': 'Server Says: '+text}, broadcast=True, include_self=False)
If you have to send messages for a specific group of clients you can create rooms and then use emit('my_event', {data:my_data}, room=my_room, include_self=False)
for sending messages to clients who joined my_room
. You can check the reference of flask_socketio.emit for more details.
https://flask-socketio.readthedocs.io/en/latest/#flask_socketio.SocketIO.emit
You're looking for the skip_sid
parameter for socketio.emit()
.
skip_sid – The session id of a client to ignore when broadcasting or addressing a room. This is typically set to the originator of the message, so that everyone except that client receive the message. To skip multiple sids pass a list.