ChatProvider is the root component for the entire chat system. It establishes and manages the Socket.io connection, fetches initial unread counts, and provides the shared socket instance to all hooks that need it.
ChatProvider must be placed inside SublayProvider and above any component that uses chat hooks.Adding ChatProvider
What ChatProvider Does
When a user is authenticated,ChatProvider:
- Opens a Socket.io connection to the Sublay server, authenticated with the user’s access token
- Fetches initial unread counts (total unread messages and count of conversations with unread messages) so badges are accurate before the conversation list is loaded
- Listens for real-time events — new messages, message edits, deletions, reactions, typing indicators, and conversation updates
- Updates Redux state automatically as events arrive, so all components using chat hooks stay in sync
Token Rotation
When the access token rotates (e.g. after a refresh),ChatProvider updates the socket’s auth credentials and forces a re-handshake. The socket is not torn down — room memberships are preserved.
Checking Connection State
UseuseChatSocket to read the connection state in your UI:
Unread Counts
Two hooks expose the unread counts fetched on mount:useTotalUnreadCount— total number of unread messages across all conversationsuseUnreadConversationCount— number of conversations with at least one unread message
Next Steps
Conversations
Create and list conversations
Messages
Send and receive messages

