Skip to content

Commit

Permalink
add mapping between client_token and socket id (#3388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor authored May 28, 2024
1 parent 6c6eaaa commit 33f71c6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,12 @@ class EventNamespace(AsyncNamespace):
# The application object.
app: App

# Keep a mapping between socket ID and client token.
token_to_sid: dict[str, str] = {}

# Keep a mapping between client token and socket ID.
sid_to_token: dict[str, str] = {}

def __init__(self, namespace: str, app: App):
"""Initialize the event namespace.
Expand All @@ -1327,7 +1333,9 @@ def on_disconnect(self, sid):
Args:
sid: The Socket.IO session id.
"""
pass
disconnect_token = self.sid_to_token.pop(sid, None)
if disconnect_token:
self.token_to_sid.pop(disconnect_token, None)

async def emit_update(self, update: StateUpdate, sid: str) -> None:
"""Emit an update to the client.
Expand All @@ -1351,6 +1359,9 @@ async def on_event(self, sid, data):
# Get the event.
event = Event.parse_raw(data)

self.token_to_sid[event.token] = sid
self.sid_to_token[sid] = event.token

# Get the event environment.
assert self.app.sio is not None
environ = self.app.sio.get_environ(sid, self.namespace)
Expand Down

0 comments on commit 33f71c6

Please sign in to comment.