I am having difficulty trying to send a message to a specific client in Flask-SocketIO. I have taken out the database logic and a few other bits of code for the sake of simplicity as I believe they are irrelevant to the problem I'm currently encountering.
EDIT 2: I solved the issue. Whenever I was connecting and the handle_user_connection function was called. I was not specifying which client the server would be emitting the user_connected message to. This can be seen in line 3 of the server side code.
Server side
@socketio.on('connect')
def handle_user_connect():
socketio.emit("user_connected", {"id":request.sid},callback=messageReceived,room=request.sid)
@socketio.on("join")
def on_join(data):
client = request.sid
room = f"{data['chatroom_name']}-{data['chatroom_id']}"
join = user_num < user_limit
if join:
join_room(room)
sessions[client] = room #sessions is an ordered dictionary of users and the rooms they join
print(room)
socketio.emit("join",room=client)
socketio.emit("room_status", {'room_status': user_num}, room=room)
else:
print(f"room {name} full.")
Client side
var socket = io.connect("http://127.0.0.1:5000" ,{'sync disconnect on unload': true });
socket.on("user_connected", function() {
socket.emit("join", {id: chatroom_id, name: chatroom_name})
});
socket.on("join",function (msg) {
prompt_username_change(msg)
});
socket.on("room_status",function (msg) {
update_user_number(msg);
});
Whenever I am attempting to emit a join message to just a client, as seen in line 10 of the server-side code, the server emits the join to all users within the room. This is seen in an example of users joining a room called 'roomtest-10'.
emitting event "join" to c557f6c6804c47e5994ef340cabd81ed [/]
c557f6c6804c47e5994ef340cabd81ed: Sending packet MESSAGE data 2["join"]
emitting event "room_status" to roomtest-10 [/]
c557f6c6804c47e5994ef340cabd81ed: Sending packet MESSAGE data 2["room_status",{"room_status":1}]
127.0.0.1 - - [26/Apr/2020 14:58:23] "POST /socket.io/?EIO=3&transport=polling&t=N6saJeC&sid=7e72f51ad61b42f189ec96d85dfe2001 HTTP/1.1" 200 -
7e72f51ad61b42f189ec96d85dfe2001 is entering room roomtest-10 [/]
emitting event "join" to 7e72f51ad61b42f189ec96d85dfe2001 [/]
Even though 7e72f51ad61b42f189ec96d85dfe2001 is already in the room, the join gets resent to them thus causing numerous problems.
EDIT: I have added another example of the server logs and added in a print statement showing the value of client when a single user tries to join whilst another is already connected.
67a17a8d161741cea1a72a0ad6c05fc4: Received packet MESSAGE data 2["join",{"id":"45","name":"roomtest"}]
received event "join" from 67a17a8d161741cea1a72a0ad6c05fc4 [/]
LOGGING USER SESSION ID: e1e0d3dd17414795b887d291c15e9218
127.0.0.1 - - [26/Apr/2020 15:34:34] "POST /socket.io/?EIO=3&transport=polling&t=N6sibpv&sid=67a17a8d161741cea1a72a0ad6c05fc4 HTTP/1.1" 200 -
67a17a8d161741cea1a72a0ad6c05fc4 is entering room roomtest-45 [/]
emitting event "join" to 67a17a8d161741cea1a72a0ad6c05fc4 [/]
67a17a8d161741cea1a72a0ad6c05fc4: Sending packet MESSAGE data 2["join"]
LOGGING USER SESSION ID: 67a17a8d161741cea1a72a0ad6c05fc4
127.0.0.1 - - [26/Apr/2020 15:34:34] "GET /socket.io/?EIO=3&transport=polling&t=N6sibpy&sid=67a17a8d161741cea1a72a0ad6c05fc4 HTTP/1.1" 200 -
emitting event "room_status" to roomtest-45 [/]
Thanks in advance!