Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi forum,

there's an application ("the backend") using python flask socket.io. Multiple browsers ("the frontends") may connect to this application at the same time. One of the use cases requires that the backend sends a message to one of the frontends and to only this one frontend.

To distinguish one frontend from another, we use SocketIO.send() and its concept of rooms.
Python
socketSendMethod(
    data = {
        "ToolId" : self._toolId,
        "ToolType" : self.ToolType.name,
        "Value" : measurement
    },
    room = oneSession.Id

The room here is a session id that is generated and sent to the frontend upon user login. The frontend then stores this id in a cookie. It then triggers an event handler which uses the session id (called "room") as argument to join_room():
Python
@socketio.on("join")
def join(message):
    join_room(int(message["room"]))

The join_room() method doesn't work outside of event handlers.

So we have a session id, have it stored in the backend and get its value as a cookie every time the frontend calls some route
Python
@app.route("/", methods=["GET"]
def routeRoot():
    return(render_template("index.html"))

This way, so I thought, we could specify exactly which frontend to send a message to. But the message gets transferred to all the connected frontends, just like without mentioning the room in SocketIO.send() at all.

Digging into the problem, I found that send() doesn't know a "room" parameter. It uses "to" instead. But after fixing this, none of the connected clients gets the message. Digging deeper, I stumbled over BaseManager.emit() which is called some steps into the stack and checks if (this one is new) namespace exists in its self.rooms list, which is empty.

My question is: What should I do to populate the rooms list?

[Update]
There seems to be an issue with the
Python
@socketio.on("join")
def join(message):
    join_room(int(message["room"]))

method. It doesn't get called. Except for seldom occurrences when it does. Not getting called is something I would suppose to have influence on the list of known rooms and therefore on my problem.

What can go wrong (mabe even on the frontend side?) so that an event like the one above doesn't get called? How can we fix this?

What I have tried:

Coding, running the code, logging into the application, debugging.
Posted
Comments
[no name] 9-Mar-22 12:00pm    
Wouldn't an IP address be more useful?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900