Architecture
Realtime consists of several key components:- WebSocket Server: Elixir-based server handling persistent connections
- Postgres Replication: Listens to database changes via logical replication
- Phoenix Channels: Manages pub/sub messaging and presence
- Global Distribution: Servers deployed across multiple regions for low latency
- Authorization: Integrates with Supabase Auth and RLS policies
Core Features
Realtime provides three main features:1. Broadcast
Send and receive low-latency messages between clients. Use cases: Chat messages, cursor tracking, collaborative editing, game events2. Presence
Track and synchronize user state across clients. Use cases: Online user lists, typing indicators, active participants3. Postgres Changes
Listen to database changes in real-time. Use cases: Live data updates, notification systems, activity feedsBroadcast
Broadcast enables low-latency messaging between connected clients:Basic Broadcast
Chat Application
Collaborative Cursors
Broadcast from Database
Trigger broadcasts directly from PostgreSQL:Presence
Presence tracks and synchronizes user state across all connected clients:Basic Presence
Online User List
Typing Indicators
Postgres Changes
Subscribe to database changes in real-time:Listen to All Changes
Listen to Inserts
Listen to Updates
Listen to Deletes
Filter by Column Values
Multiple Subscriptions
Channel Management
Channel States
Unsubscribing
Authorization
RLS Policies for Realtime
Realtime respects Row Level Security policies:Private Channels
Custom Authorization
Performance Optimization
Throttling Updates
Connection Pooling
Selective Subscriptions
Best Practices
Use Filters
Filter subscriptions to reduce unnecessary data transfer.
Throttle Updates
Debounce high-frequency events to prevent overwhelming clients.
Clean Up
Always unsubscribe from channels when components unmount.
Handle Errors
Implement proper error handling and reconnection logic.
Common Patterns
Live Query
Optimistic Updates
Next Steps
Broadcast
Learn about low-latency messaging
Presence
Track user state across clients
Postgres Changes
Subscribe to database changes
Authorization
Secure your real-time channels
