Skip to main content

Overview

useChat enables real-time messaging between users in your collaborative application. It provides a complete chat system with message history, automatic user identification, and timestamp tracking. Perfect for building chat interfaces, comments systems, and any collaborative communication feature.
Perfect for: Real-time chat applications, team communication, comment systems, collaborative feedback, customer support chat, and any feature requiring user-to-user messaging.

Basic Usage

Signature

Parameters

string
required
Unique identifier for this chat instance. Use different keys for separate chat rooms or conversations.

Return Value

ChatMessage[]
Array of all messages in the chat, ordered chronologically
(message: string) => void
Function to send a new message to the chat

ChatMessage Interface

number
Unique identifier for the message
string
User ID of the user who sent the message
string
The text content of the message
number
Timestamp (in milliseconds) when the message was sent

Examples

Modern Chat Interface

Multi-Room Chat System

Chat with Rich Features

Minimal Comment System

Best Practices

Performance Considerations

  • Unique Keys: Use descriptive, unique rtKey values for different chat instances
  • Message Limits: Consider implementing message history limits for long-running chats
  • Auto-scroll: Implement smooth auto-scrolling for better user experience

User Experience

  • Message Formatting: Support line breaks and basic formatting in messages
  • Timestamps: Always display timestamps for better context
  • User Identification: Combine with useNicknames for better user identification
  • Typing Indicators: Add typing indicators for real-time feedback

Security & Moderation

TypeScript Support

The hook is fully typed with comprehensive TypeScript support: