Overview
Broadcast allows you to send low-latency messages between connected clients without storing them in the database. It’s perfect for real-time chat, multiplayer games, cursor positions, and any use case where you need fast, ephemeral communication.Broadcast messages are not persisted. They only exist in memory and are delivered to currently connected clients.
Key Features
- Low Latency: Broadcast has minimal overhead compared to database writes
- Ephemeral: Messages aren’t stored, reducing database load
- Flexible: Send any JSON-serializable data
- Scalable: Handles high message volumes efficiently
Basic Usage
Send Messages
Send a broadcast message to all clients subscribed to the channel:Receive Messages
Listen for broadcast messages:Complete Chat Example
Here’s a real-world chat implementation based on the source examples:Advanced Patterns
Multiple Event Types
Handle different message types in the same channel:Self-Broadcast Configuration
By default, you don’t receive your own messages. Enable self-broadcast to receive them:Collaborative Cursor Example
Track cursor positions in a collaborative editor:Real-World Example: Authorization Chat
This example is based on thenextjs-authorization-demo in the source repository:
Private Channels
Secure your broadcast channels with authorization:Performance Optimization
Throttle High-Frequency Updates
Avoid overwhelming clients with too many messages:Batch Related Updates
Combine multiple updates into one message:Message Size Limits
Keep broadcast messages small (< 100KB). Large messages increase latency and network usage.
Combining with Other Features
Broadcast + Presence
Use broadcast for messages and presence for online status:Broadcast + Postgres Changes
Persist important messages, use broadcast for ephemeral ones:Error Handling
Next Steps
Presence
Track online users and synchronize state
Postgres Changes
Listen to database changes in real-time
