Skip to main content
ChatProvider handles all real-time communication automatically. This guide explains what happens behind the scenes and which hooks you use to expose real-time state in your UI.

How Real-time Works

ChatProvider opens a single Socket.io connection for the authenticated user. All components using chat hooks share this one connection. The provider listens for server events and updates Redux state, so any component reading from the hooks will re-render with up-to-date data.

Socket Events Handled Automatically

Live Conversation List

The conversation list returned by useConversations is fully live — you do not need to refetch it manually:
  • Reorders to the top when a new message arrives in any conversation, across direct, group, and space chats.
  • Inserts new conversations as they appear — a DM someone starts with you, a group you’re added to, or a paginated-out conversation that just received a message.
  • Removes conversations you leave, are removed from, or that are deleted.
  • Self-heals on reconnect. After a dropped socket reconnects, the list and unread counts reconcile against the server automatically, correcting anything missed while offline (deep pagination resets to the first page).

Missed Messages After a Reconnect

When the socket reconnects, every mounted ConversationProvider re-joins its conversation and loads the messages sent while it was offline, page by page from the newest message loaded when the connection dropped until none are left, so a long disconnect doesn’t drop any. If no message was loaded yet (an empty conversation, or a first load that failed), it loads the latest page instead, as on mount. Open threads (useMessageThread, or useLiveChatMessages with parentMessageId) catch up the same way: they load the replies sent while offline, from the last reply they had received in order before the connection dropped (so a reply you send while offline doesn’t hide the ones you missed), or the thread’s first page if none was loaded. A thread you haven’t scrolled to the end of needs no catch-up, since loadMore brings in the newer replies. Catch-up loads new messages and replies. Edits, deletions and reactions made while offline show up the next time the conversation or thread loads those messages, for example when it is opened again.

Connection State

Use useChatSocket to read whether the socket is connected:
See useChatSocket.

Unread Counts

ChatProvider fetches unread counts on mount and keeps them in sync via socket events, including for conversations not currently loaded in the list. Counts also reconcile against the server on socket reconnect.
When an event touches a conversation not currently loaded in the list (e.g. paginated out), both useTotalUnreadCount and useUnreadConversationCount reconcile automatically against the server’s authoritative unread summary — no manual refresh or remount is required.

Read Receipts

ConversationProvider marks its conversation as read for you. When it mounts, the conversation’s unread count is cleared right away, and whenever the newest loaded message changes (initial load, a live message, your own confirmed send, or messages caught up after a reconnect) the read position on the server advances to it. Loading older pages doesn’t mark anything, and optimistic messages are skipped until the server confirms them. Mounting a ConversationProvider therefore means “the user is viewing this conversation” — don’t mount one for a conversation that isn’t on screen. To mark read yourself outside a ConversationProvider, call useMarkConversationAsRead. It clears the unread count in Redux immediately (optimistic) and sends the mark-as-read request to the server.
See useMarkConversationAsRead.

Typing Indicators

useTypingIndicator manages both sending typing events to other users and receiving typing events from them.

Sender Protocol

  • Call startTyping() on each keystroke. The hook throttles the emit to every 2 seconds internally — you do not need to debounce.
  • Call stopTyping() when the user sends the message, clears the input, or blurs the field.
  • The hook emits typing:stop automatically on unmount if the user was typing.

Receiver Side

typingUsers is an array of user IDs currently typing in this conversation. The current user is excluded. Each typing user is auto-removed after 5 seconds of inactivity (no keep-alive event from the server). See useTypingIndicator.

Reporting Messages

Users can report messages for moderation review:
Message reports use the same hook as entity and comment reports. targetId is the message id — the conversation is resolved server-side. See useCreateReport.