Files
supabase/apps/ui-library/content/docs/tanstack/realtime-chat.mdx
T
Ivan Vasilov 429aa9d084 fix: More fixes for UI Library (#34552)
* Add realtime chat to the landing page.

* Fix the realtime cursor demo on the landing page.

* Fix a bad spelling of shadcn.

* Fix the wrong link in RR auth.

* Change the demo for reatlime cursor to use predefined names.

* Fix the React Router auth block docs.

* Regenerate the realtime chat code.

* Add a docs field to the clients blocks. Add a missing package to the nextjs client block.

* Add links to additional docs on the auth blocks pages.

* Add additional links to the current user avatar docs.

* Add additional links to the client docs.

* Add additional links to the dropzone docs.

* Add additional links to the avatar stack.

* Add additional links to the realtime cursor.

* Add additional links to the realtime chat.

* Fix the dropzone links.
2025-03-31 18:40:03 +08:00

104 lines
3.0 KiB
Plaintext

---
title: Realtime Chat
description: Real-time chat component for collaborative applications
---
<div className="flex flex-row -space-x-px w-full">
<BlockPreview name="realtime-chat-demo" isPair />
<BlockPreview name="realtime-chat-demo" isPair />
</div>
## Installation
<BlockItem
name="realtime-chat-tanstack"
description="Renders a real-time chat interface for users in a shared room."
/>
## Folder structure
<RegistryBlock itemName="realtime-chat-tanstack" />
## 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)