How hmchangw/chat works
1. This repo is a distributed multi-site chat system in Go, with a React/Vite frontend (chat-frontend) and a set of independently deployable backend services and workers. The architecture is centered on HTTP for user-facing APIs and NATS for internal RPC/event delivery.
2. The frontend is a single-page app that handles login, room navigation, message composition, threads, search, member management, and debug tooling. It talks to backend APIs through the chat-frontend/src/api layer and subscribes to live updates through NATS-aware contexts (NatsContext, RoomEventsContext, ThreadEventsContext, RoomKeysContext). It also contains local crypto helpers for encrypted rooms.
3. Authentication is split out into auth-service, which integrates with Keycloak/OIDC. The repo includes a Keycloak realm export and deploy templates, so identity is treated as an external boundary: the frontend authenticates through OIDC, then backend services rely on JWT-bearing requests or NATS auth flows.
4. The core message write path is event-driven. message-gatekeeper validates inbound message actions and membership/subscription state, then message-worker persists canonical message data and message-related state to Cassandra and MongoDB. From there, NATS events fan out to downstream consumers such as broadcast, notification, search, and inbox workers.
5. broadcast-worker is the realtime fanout layer. It consumes message and room events, coalesces them, resolves keys/metadata, and publishes to the subjects the frontend listens to. This is where user-facing live updates are turned into per-room or per-user event streams.
6. history-service is the read-side service for message history, threads, pins, reactions, and room-time queries. Its Cassandra repository is the main persistence boundary for message history, while MongoDB is used for supporting metadata like rooms, subscriptions, thread rooms, and custom emoji. The service exposes read APIs that the frontend uses for scrolling history, thread panes, and message actions.
7. Room and user metadata are handled by separate service boundaries. room-service manages room state, membership-related operations, and room-specific orchestration. user-service manages the user-centric view: profiles, subscriptions, thread unread state, app state, and cross-service aggregation through clients to history, presence, and room services.
8. Search is a dedicated pipeline. search-sync-worker consumes canonical domain events and transforms them into search documents, while search-service serves query APIs for messages, rooms, users, and apps. The search stack has both Mongo-backed operational state and an Elasticsearch/OpenSearch-style index backend, with Valkey used for coordination/cache in some paths.
9. Attachments, avatars, and emoji are handled separately from chat content. upload-service stores file uploads and previews, typically using MinIO as the object store. media-service serves avatars and custom emoji assets, with its own cache and object-storage integration, so binary media does not flow through the messaging stores.
10. Presence is its own subsystem. user-presence-service tracks live user presence, includes a sweeper for stale state, and has a sync subcomponent for reconciling shared presence across nodes/sites. It uses a dedicated store plus peer-to-peer coordination, with Valkey appearing in the sync path.
11. The system uses multiple stores by design: Cassandra for high-volume message history and thread data, MongoDB for relational-ish application state, Valkey for caches and coordination, MinIO for media, and search index storage for fast lookup. The shared pkg/ tree contains reusable infrastructure around these boundaries: NATS helpers, error codes, Cassandra/Mongo/Valkey utilities, room key storage/sender logic, at-rest encryption, and domain models.
12. Cross-site and migration concerns are first-class. The data-migration directory contains oplog connector/transformer/direct-transfer services for moving or mirroring MongoDB data into the new model. The docs/specs/botplatform and many docs/superpowers plans show the project has explicit cutover, federation, and subject-naming design work, which is why NATS subject structure and migration services are part of the architecture rather than afterthoughts.
13. Operationally, every service has container and pipeline definitions under deploy/, with Azure Pipelines used across the repo. Local development is supported through docker-local, and observability/testing tooling includes Prometheus, Grafana, Jaeger, load generation, and NATS debug utilities. Those tools matter because the architecture is intended to be exercised as a distributed system, not as isolated binaries.
14. If you are drawing the architecture diagram, the main boundaries are: client/UI, auth, command/query services, realtime/event bus, storage backends, search/indexing, media, presence, and migration/ops tooling. The most important arrows are frontend to HTTP APIs, services to NATS, NATS to workers, and workers to the various data stores and indexes.
Main entry points: chat-frontend Chat frontend · message-worker Message worker · broadcast-worker Broadcast worker · history-service History service
Components 21
Client UI
React/Vite single-page chat client and live-update contexts
- Chat frontend · React/Vite app for login, rooms, threads, search, members, and debug tooling; uses HTTP APIs and live NATS-aware contexts.
chat-frontend
Core Services
HTTP APIs for auth, rooms, history, users, search, media, upload, portal, and presence
- Auth service · Login and token boundary integrated with Keycloak/OIDC; issues/validates auth for the rest of the system.
auth-service - Room service · Room state, membership orchestration, and room-specific read/write coordination.
room-service - History service · Read-side service for message history, threads, pins, reactions, and room-time queries.
history-service - User service · User-centric aggregation for profiles, subscriptions, thread unread state, and app state across services.
user-service - Search service · Search endpoints for messages, rooms, users, and apps over operational state plus the search index.
search-service - Upload service · Attachment upload and preview boundary backed by object storage and metadata stores.
upload-service - Media service · Serves avatars and custom emoji assets with cache and object-storage integration.
media-service - Portal service · Auxiliary service for portal/settings-style application data and orchestration.
portal-service - Presence service · Tracks live user presence and exposes presence state to other services.
user-presence-service
Workers
NATS consumers and background processors for fanout, writes, search sync, inbox, and migration
- Message gatekeeper · Validates inbound message actions, membership, and subscription state before writes proceed.
message-gatekeeper - Message worker · Persists canonical message and message-related state to Cassandra and MongoDB.
message-worker - Broadcast worker · Consumes message and room events, coalesces them, and publishes live update subjects for clients.
broadcast-worker - Notification worker · Consumes domain events and routes notifications, member updates, presence hooks, and thread-related signals.
notification-worker - Search sync worker · Transforms canonical domain events into search documents and keeps the search index in sync.
search-sync-worker - Inbox worker · Background consumer for inbox/member-related projections and downstream delivery state.
inbox-worker - Room worker · Room-side background processing for subscriptions, keys, and system-message style fanout.
room-worker - Data migration · Oplog connector, transformer, and direct-transfer services for moving and mirroring MongoDB-era data.
data-migration
Shared Platform
Reusable packages, domain models, and local/dev tooling
- pkg · Reusable infrastructure: NATS, errcodes, stores, room keys, at-rest encryption, search, and domain models.
pkg - Tools · Local dev, observability, load generation, NATS debug, profiling, and sample-data seeding tools.
tools - Docker local · Local dependency and service compositions for development and migration demos.
docker-local
Connections 27
- Chat frontend → Auth service: OIDC login · Browser authentication and session bootstrap flow.
- Chat frontend → Room service: room APIs · Room navigation, membership actions, and room metadata operations.
- Chat frontend → History service: history reads · Scrollback, thread panes, pins, reactions, and room-time queries.
- Chat frontend → User service: user APIs · Subscriptions, unread state, app state, and aggregated user views.
- Chat frontend → Search service: search queries · Global and in-room search for messages, rooms, and users.
- Chat frontend → Upload service: attachments · File and preview upload flows.
- Chat frontend → Media service: avatars/emoji · Binary media and custom emoji delivery.
- Chat frontend → Presence service: presence views · Live presence indicators and updates.
- Chat frontend → Broadcast worker: live updates · NATS-aware contexts subscribe to realtime subjects published by fanout.
- Message gatekeeper → History service: read checks · Validates message actions against history and room-time state.
- Message gatekeeper → Room service: membership checks · Verifies room membership and subscription state before accepting writes.
- Message gatekeeper → Message worker: approved writes · Forwards validated message commands into the persistence pipeline.
- Message worker → pkg: store/model libs · Uses shared repositories, models, encryption, and NATS helpers.
- Message worker → Broadcast worker: domain events · Emits canonical message events for realtime fanout.
- Message worker → Notification worker: domain events · Feeds notification and thread-related downstream consumers.
- Message worker → Search sync worker: index events · Publishes canonical events for search document updates.
- Message worker → Inbox worker: inbox events · Delivers member/inbox projection events.
- Message worker → History service: persisted history · Writes canonical message data consumed by the read-side service.
- Broadcast worker → Chat frontend: NATS subjects · Publishes room/user/thread update streams consumed by the client.
- Search sync worker → Search service: indexed docs · Keeps search query data current.
- Room worker → Broadcast worker: room events · Room-side changes are turned into live fanout events.
- Presence service → User service: presence state · User service aggregates live presence into user-facing views.
- Data migration → History service: migrated data · Supports cutover and historical data movement into the new model.
- Data migration → Room service: mirrored metadata · Migrates room and subscription-related data.
- Tools → Chat frontend: dev/test · Smoke tests, local observability, load generation, and debug utilities.
- Tools → Broadcast worker: load/debug · Exercises fanout behavior and NATS traffic.
- Docker local → pkg: local deps · Provides local Cassandra, MongoDB, Valkey, NATS, and related stacks for development.