Files
supabase/apps/ui-library/content/docs/react-router/realtime-chat.mdx
Kody JacksonandClaude Sonnet 5.5 701730e3eb chore(kb,www,docs) - Add in relevant cross-links between the Supabase Component Library and kb,www,docs (#53237)
## Problem

After #53183 and #52567 have been addressed, we should strengthen the
interconnections between the UI library and the rest of the Supabase
ecosystem.

## Solution

- Adds contextual links for `/library`, Auth / Realtime category pages,
and specific Auth / Realtime components. Hits top-level product pages,
docs, kb, and blogs.

## Preview links

N/A, link changes are relatively atomic so easier to review without many
preview links.

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 08:37:14 -06:00

103 lines
3.4 KiB
Plaintext

---
title: Realtime Chat
description: Real-time chat component for collaborative applications
preview: |-
<BlockOverview name="realtime-chat-react-router" showFiles>
<DualRealtimeChat />
</BlockOverview>
---
## Installation
<BlockItem name="realtime-chat-react-router" />
## Introduction
The Realtime Chat component provides a complete chat interface that enables users to exchange messages in real-time within a shared room.
## Usage
### Basic usage
```tsx
import { RealtimeChat } from '@/components/realtime-chat'
export default function ChatPage() {
return <RealtimeChat roomName="my-chat-room" username="john_doe" />
}
```
### With initial messages
```tsx
import { RealtimeChat } from '@/components/realtime-chat'
import { useMessagesQuery } from '@/hooks/use-messages-query'
export default function ChatPage() {
const { data: messages } = useMessagesQuery()
return <RealtimeChat roomName="my-chat-room" username="john_doe" messages={messages} />
}
```
### Storing messages
```tsx
import { RealtimeChat } from '@/components/realtime-chat'
import { useMessagesQuery } from '@/hooks/use-messages-query'
import { storeMessages } from '@/lib/store-messages'
export default function ChatPage() {
const { data: messages } = useMessagesQuery()
const handleMessage = (messages: ChatMessage[]) => {
// Store messages in your database
await storeMessages(messages)
}
return <RealtimeChat roomName="my-chat-room" username="john_doe" onMessage={handleMessage} />
}
```
## Features
- Real-time message synchronization
- Message persistence support with `onMessage` and `messages` props
- Customizable message appearance
- Automatic scroll-to-bottom on new messages
- Room-based isolation for scoped conversations
- Low-latency updates using Supabase Realtime
## Props
| Prop | Type | Description |
| ------------ | ----------------------------------- | ------------------------------------------------------------- |
| `roomName` | `string` | Unique identifier for the shared chat room. |
| `username` | `string` | Name of the current user; used to identify message senders. |
| `onMessage?` | `(messages: ChatMessage[]) => void` | Optional callback to handle messages, useful for persistence. |
| `messages?` | `ChatMessage[]` | Optional initial messages to display in the chat. |
### ChatMessage type
```typescript
interface ChatMessage {
id: string
content: string
user: {
name: string
}
createdAt: string
}
```
## Further reading
- [Realtime Broadcast](https://supabase.com/docs/guides/realtime/broadcast)
- [Realtime authorization](https://supabase.com/docs/guides/realtime/authorization)
- [Getting started with Realtime](https://supabase.com/docs/guides/realtime/getting_started)
- [Broadcast from the database](https://supabase.com/docs/guides/realtime/broadcast#broadcast-from-the-database)
- [Broadcast replay](https://supabase.com/docs/guides/realtime/broadcast#broadcast-replay)
- [Realtime limits](https://supabase.com/docs/guides/realtime/limits)
- [Realtime message usage and billing](https://supabase.com/docs/guides/platform/manage-your-usage/realtime-messages)
- [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security)
- [multiplayer.dev, a live Realtime demo](https://multiplayer.dev)