What are the patterns for building real-time features in custom software?
Real-time functionality delivers instant updates to connected clients. Technology options: WebSockets (bidirectional, persistent connections for chat, collaboration), Server-Sent Events (SSE) (unidirectional server-to-client for notifications, feeds), Supabase Realtime (built-in PostgreSQL change notifications over WebSocket), and long polling (fallback for environments that don't support WebSockets). Implementation with Supabase: subscribe to database changes using Supabase Realtime channels, implement presence tracking for online user indicators, and use broadcast for ephemeral events (typing indicators, cursor positions). Architecture: WebSocket connections terminate at a real-time server (or Supabase manages this), the server subscribes to relevant data changes, and pushes updates to connected clients filtered by their permissions. Scaling: WebSocket connections are stateful, requiring sticky sessions or a pub/sub backbone (Redis Pub/Sub) for multi-server deployments. Supabase Realtime handles this automatically. Common real-time features: collaborative editing (use CRDTs or OT algorithms), live notifications (new messages, mentions, status changes), real-time dashboards (streaming metrics and KPIs), and presence indicators (who's online, who's viewing this page). Optimization: implement debouncing for high-frequency updates, use binary protocols (MessagePack) for large payloads, implement reconnection logic with exponential backoff, and buffer updates during disconnection for replay on reconnection.