Templates/Realtime Chat System

Realtime Chat System

RealtimeProductionRealtime collaboration

Realtime messaging system with WebSocket channels, pub/sub presence tracking, durable message history, media uploads, and error telemetry. Use for chat products, collaborative tools, or live feeds.

Recommended for: Product teams building live messaging

8 nodes9 connectionsWebSocket transportPresence stateDurable history

Scope Notes

Intended for Product teams building live messaging at Production complexity and Realtime collaboration scope. Treat the graph as a starting shape rather than a fixed stack: keep the topology and the signals below, then swap providers to match your platform.

WebSocket transportPresence stateDurable history

Review Checklist

0 of 12 reviewed

Use Case

Team messaging apps, customer support chat, community platforms, collaborative tools

Best Fit Scenarios

  • Support chat with persistent history
  • Community channels with presence signals
  • Collaborative apps with low-latency updates

Stack Breakdown

ReactWebSocket GatewayRedisMongoDBS3Sentry

Architecture Layers

1Client
2WebSocket gateway
3Domain services
4Pub/sub backbone
5Persistence & media
6Error visibility

Components by Category

frontend

React

backend

WebSocket GatewayChat ServicePresence Service

database

RedisMongoDB

infra

S3

external

Sentry

Why This Topology Works

The WebSocket gateway holds persistent connections while Redis pub/sub distributes messages across gateway instances. Presence is tracked separately from chat logic so each scales independently, MongoDB stores durable history, and S3 holds attachments.

Scaling Notes

Gateway instances scale horizontally behind Redis pub/sub. MongoDB shards by room and presence state is ephemeral in Redis with TTL cleanup.

Observability

Track connection count, message throughput and pub/sub lag. Gateway and Chat Service errors flow to Sentry so a broken fan-out is visible immediately.

Typical Bottlenecks

  • Fan-out cost for very large rooms
  • MongoDB write throughput on message history
  • Attachment upload size and virus-scanning backlog

Async Flow and Reliability

Messages are written to the gateway synchronously, then fanned out through Redis pub/sub to every instance and persisted asynchronously. Media uploads are queued rather than blocking the message send.

Upgrade Path

Add authentication on the WebSocket handshake, a per-user rate limiter, and a push provider for offline recipients; then move attachment bytes to a presigned direct-to-S3 upload.