Feat/session reconnection
Current Scenario
- The chat socket connection is created between server and client at the moment of initial connection.
- Server keeps sending updates via the fixed connection via the respective connection's
request.sid
Problem If the user reloads the page, leaves the page or checks the same session on a different device they will not be able to see live updates
Proposed Solution
- The user joins a room with the
session_idwe preserve in oursessionstable - The message are directed to a specific
roomwhich corresponds to thesession_id - Since the user will join the same room, even in multiple reconnections, the live updates are still available
Summary by CodeRabbit
-
New Features
- Session-scoped real-time chat: clients join a session room and messages/updates are delivered only to that room.
- Explicit "join session" support with confirmation; improved connect/disconnect handling.
- New public method to update session state and reflect changes in real time.
-
Refactor
- Emissions now broadcast to session rooms with clearer logging.
- Errors include session context for easier troubleshooting.
- Message history persistence ensured after publish to keep conversations in sync.
Walkthrough
Session-scoped Socket.IO rooms added and used for broadcasting; ChatNamespace gains connect/join/disconnect handlers and requires/joins a session_id for chat events. Message emitters now attempt real-time emission before persisting messages. Session exposes a new public update(**kwargs) -> bool to persist session attributes.
Changes
| Cohort / File(s) | Summary |
|---|---|
Session emissions & APIbackend/director/core/session.py |
InputMessage and OutputMessage publishing now emit to room=self.session_id in namespace "/chat" with try/except logging; persistence via db.add_or_update_msg_to_conv(**self.model_dump()) is ensured after emission attempts. New public method Session.update(self, **kwargs) -> bool added to persist session attributes via db.update_session. |
Socket.IO namespace room handlingbackend/director/entrypoint/api/socket_io.py |
ChatNamespace adds on_connect, on_join_session, on_disconnect; on_chat now requires session_id, calls join_room(session_id) and emits an error if missing, then invokes existing chat handling. Imports from flask_socketio extended to include join_room and emit. |
Sequence Diagram(s)
sequenceDiagram
autonumber
participant C as Client
participant NS as ChatNamespace (Socket.IO)
participant H as ChatHandler
participant S as Session
participant IO as Socket.IO Server
participant DB as DB
Note over C,NS: Client sends chat with session_id
C->>NS: on_chat({ session_id, text, ... })
NS->>IO: join_room(session_id)
NS->>H: handle(message)
H->>S: publish/update(message)
S->>IO: emit to room=session_id (namespace "/chat")
S->>DB: add_or_update_msg_to_conv(...)
IO-->>C: room-scoped message/event delivered
alt Missing session_id
NS-->>C: emit error "session_id required"
end
sequenceDiagram
participant C as Client
participant NS as ChatNamespace
participant IO as Socket.IO Server
C->>NS: on_join_session({ session_id })
NS->>IO: join_room(session_id)
NS-->>C: emit joined confirmation (room=session_id)
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
I hop between the rooms tonight,
I call "join_room" beneath soft light,
I toss a byte, it finds its nest,
DB burrows hold each guest.
Carrots, logs, and events align—🥕✨
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 77.78% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title Check | ✅ Passed | The title "Feat/session reconnection" accurately and concisely reflects the primary change — moving socket updates to session/room-based handling so users can reconnect and continue receiving live updates; it is specific and directly related to the PR objectives. The phrasing is short, clear, and avoids unnecessary detail or noise. |
✨ Finishing touches
- [ ] 📝 Generate Docstrings
🧪 Generate unit tests
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.