Skip to content
+

Chat - Real-time thread sync

Push add, update, and remove events from your backend to keep conversations and messages in sync.

Collection events drive structural changes to the message and conversation lists, in contrast to the typing, presence, and read-state events covered in the basic Realtime demo:

  • message-added—a new message appears in the thread.
  • message-updated—an existing message is modified.
  • message-removed—a message is deleted from the thread.
  • conversation-added—a new conversation appears in the sidebar.
  • conversation-updated—a conversation title or metadata changes.
  • conversation-removed—a conversation is deleted (active conversation resets if it matched).

Key concepts

Dispatching collection events

Push events through the onEvent callback provided by subscribe():

// Add a message from another user
onEvent({
  type: 'message-added',
  message: {
    id: 'msg-new',
    conversationId: 'support',
    role: 'assistant',
    parts: [{ type: 'text', text: 'New message from the backend.' }],
    status: 'sent',
  },
});

// Remove a conversation
onEvent({
  type: 'conversation-removed',
  conversationId: 'old-thread',
});

Active conversation reset

When a conversation-removed event arrives and the removed conversation is the active one, the runtime resets activeConversationId to undefined. The UI can respond by showing a placeholder or selecting the next conversation.

The demo below shows collection events driving thread and sidebar updates in real time:

Realtime collection sync

Message and conversation events can reshape the thread without a manual refetch.

Support
Product
Conversations

2

Active

Support

Messages

4

MUI Agent

Welcome to the headless runtime demo.

Alice

Show me the smallest possible chat setup.

MUI Agent

The controlled API keeps public state array-first.

Sam

That is the behavior we want to document.

Press Enter to start editing

Key takeaways

  • Collection events (*-added, *-updated, *-removed) drive structural changes to the store.
  • The runtime handles normalization—adding, replacing, or removing records in the ID maps.
  • Active conversation automatically resets when its conversation is removed.
  • These events work alongside the streaming lifecycle—you can push messages while a stream is active.

See also

API