# hmchangw/chat architecture

> Architecture diagram of the GitHub repository hmchangw/chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hmchangw/chat
- Repository: https://github.com/hmchangw/chat

## Overview

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`](https://github.com/hmchangw/chat/tree/main/chat-frontend) (Chat frontend), [`message-worker`](https://github.com/hmchangw/chat/tree/main/message-worker) (Message worker), [`broadcast-worker`](https://github.com/hmchangw/chat/tree/main/broadcast-worker) (Broadcast worker), [`history-service`](https://github.com/hmchangw/chat/tree/main/history-service) (History service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client UI"]
  node_chat_frontend["Chat frontend<br/>SPA client"]
end

subgraph group_core["Core Services"]
  node_auth_service["Auth service<br/>OIDC gateway"]
  node_room_service["Room service<br/>room API"]
  node_history_service["History service<br/>history API"]
  node_user_service["User service<br/>user API"]
  node_search_service["Search service<br/>query API"]
  node_upload_service["Upload service<br/>media upload"]
  node_media_service["Media service<br/>avatar/emoji serve"]
  node_portal_service["Portal service<br/>portal API"]
  node_presence_service["Presence service<br/>presence API"]
end

subgraph group_workers["Workers"]
  node_message_gatekeeper["Message gatekeeper<br/>command guard"]
  node_message_worker["Message worker<br/>message writer"]
  node_broadcast_worker["Broadcast worker<br/>realtime fanout"]
  node_notification_worker["Notification worker<br/>notification fanout"]
  node_search_sync_worker["Search sync worker<br/>indexer worker"]
  node_inbox_worker["Inbox worker<br/>inbox projector"]
  node_room_worker["Room worker<br/>room projector"]
  node_data_migration["Data migration<br/>migration pipeline"]
end

subgraph group_shared["Shared Platform"]
  node_pkg["pkg<br/>shared libs"]
  node_tools["Tools<br/>ops tooling"]
  node_docker_local["Docker local<br/>dev stack"]
end

node_chat_frontend -->|"OIDC login"| node_auth_service
node_chat_frontend -->|"room APIs"| node_room_service
node_chat_frontend -->|"history reads"| node_history_service
node_chat_frontend -->|"user APIs"| node_user_service
node_chat_frontend -->|"search queries"| node_search_service
node_chat_frontend -->|"attachments"| node_upload_service
node_chat_frontend -->|"avatars/emoji"| node_media_service
node_chat_frontend -->|"presence views"| node_presence_service
node_chat_frontend -->|"live updates"| node_broadcast_worker
node_message_gatekeeper -->|"read checks"| node_history_service
node_message_gatekeeper -->|"membership checks"| node_room_service
node_message_gatekeeper -->|"approved writes"| node_message_worker
node_message_worker -->|"store/model libs"| node_pkg
node_message_worker -->|"domain events"| node_broadcast_worker
node_message_worker -->|"domain events"| node_notification_worker
node_message_worker -->|"index events"| node_search_sync_worker
node_message_worker -->|"inbox events"| node_inbox_worker
node_message_worker -->|"persisted history"| node_history_service
node_broadcast_worker -->|"NATS subjects"| node_chat_frontend
node_search_sync_worker -->|"indexed docs"| node_search_service
node_room_worker -->|"room events"| node_broadcast_worker
node_presence_service -->|"presence state"| node_user_service
node_data_migration -.->|"migrated data"| node_history_service
node_data_migration -.->|"mirrored metadata"| node_room_service
node_tools -.->|"dev/test"| node_chat_frontend
node_tools -.->|"load/debug"| node_broadcast_worker
node_docker_local -.->|"local deps"| node_pkg

click node_chat_frontend "https://github.com/hmchangw/chat/tree/main/chat-frontend"
click node_auth_service "https://github.com/hmchangw/chat/tree/main/auth-service"
click node_room_service "https://github.com/hmchangw/chat/tree/main/room-service"
click node_history_service "https://github.com/hmchangw/chat/tree/main/history-service"
click node_user_service "https://github.com/hmchangw/chat/tree/main/user-service"
click node_search_service "https://github.com/hmchangw/chat/tree/main/search-service"
click node_upload_service "https://github.com/hmchangw/chat/tree/main/upload-service"
click node_media_service "https://github.com/hmchangw/chat/tree/main/media-service"
click node_portal_service "https://github.com/hmchangw/chat/tree/main/portal-service"
click node_presence_service "https://github.com/hmchangw/chat/tree/main/user-presence-service"
click node_message_gatekeeper "https://github.com/hmchangw/chat/tree/main/message-gatekeeper"
click node_message_worker "https://github.com/hmchangw/chat/tree/main/message-worker"
click node_broadcast_worker "https://github.com/hmchangw/chat/tree/main/broadcast-worker"
click node_notification_worker "https://github.com/hmchangw/chat/tree/main/notification-worker"
click node_search_sync_worker "https://github.com/hmchangw/chat/tree/main/search-sync-worker"
click node_inbox_worker "https://github.com/hmchangw/chat/tree/main/inbox-worker"
click node_room_worker "https://github.com/hmchangw/chat/tree/main/room-worker"
click node_data_migration "https://github.com/hmchangw/chat/tree/main/data-migration"
click node_pkg "https://github.com/hmchangw/chat/tree/main/pkg"
click node_tools "https://github.com/hmchangw/chat/tree/main/tools"
click node_docker_local "https://github.com/hmchangw/chat/tree/main/docker-local"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_chat_frontend toneBlue
class node_auth_service,node_room_service,node_history_service,node_user_service,node_search_service,node_upload_service,node_media_service,node_portal_service,node_presence_service toneAmber
class node_message_gatekeeper,node_message_worker,node_broadcast_worker,node_notification_worker,node_search_sync_worker,node_inbox_worker,node_room_worker,node_data_migration toneMint
class node_pkg,node_tools,node_docker_local toneRose
```

## Components

### Client UI

React/Vite single-page chat client and live-update contexts

- [Chat frontend](https://github.com/hmchangw/chat/tree/main/chat-frontend): SPA client · 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](https://github.com/hmchangw/chat/tree/main/auth-service): OIDC gateway · Login and token boundary integrated with Keycloak/OIDC; issues/validates auth for the rest of the system. · `auth-service`
- [Room service](https://github.com/hmchangw/chat/tree/main/room-service): room API · Room state, membership orchestration, and room-specific read/write coordination. · `room-service`
- [History service](https://github.com/hmchangw/chat/tree/main/history-service): history API · Read-side service for message history, threads, pins, reactions, and room-time queries. · `history-service`
- [User service](https://github.com/hmchangw/chat/tree/main/user-service): user API · User-centric aggregation for profiles, subscriptions, thread unread state, and app state across services. · `user-service`
- [Search service](https://github.com/hmchangw/chat/tree/main/search-service): query API · Search endpoints for messages, rooms, users, and apps over operational state plus the search index. · `search-service`
- [Upload service](https://github.com/hmchangw/chat/tree/main/upload-service): media upload · Attachment upload and preview boundary backed by object storage and metadata stores. · `upload-service`
- [Media service](https://github.com/hmchangw/chat/tree/main/media-service): avatar/emoji serve · Serves avatars and custom emoji assets with cache and object-storage integration. · `media-service`
- [Portal service](https://github.com/hmchangw/chat/tree/main/portal-service): portal API · Auxiliary service for portal/settings-style application data and orchestration. · `portal-service`
- [Presence service](https://github.com/hmchangw/chat/tree/main/user-presence-service): presence API · 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](https://github.com/hmchangw/chat/tree/main/message-gatekeeper): command guard · Validates inbound message actions, membership, and subscription state before writes proceed. · `message-gatekeeper`
- [Message worker](https://github.com/hmchangw/chat/tree/main/message-worker): message writer · Persists canonical message and message-related state to Cassandra and MongoDB. · `message-worker`
- [Broadcast worker](https://github.com/hmchangw/chat/tree/main/broadcast-worker): realtime fanout · Consumes message and room events, coalesces them, and publishes live update subjects for clients. · `broadcast-worker`
- [Notification worker](https://github.com/hmchangw/chat/tree/main/notification-worker): notification fanout · Consumes domain events and routes notifications, member updates, presence hooks, and thread-related signals. · `notification-worker`
- [Search sync worker](https://github.com/hmchangw/chat/tree/main/search-sync-worker): indexer worker · Transforms canonical domain events into search documents and keeps the search index in sync. · `search-sync-worker`
- [Inbox worker](https://github.com/hmchangw/chat/tree/main/inbox-worker): inbox projector · Background consumer for inbox/member-related projections and downstream delivery state. · `inbox-worker`
- [Room worker](https://github.com/hmchangw/chat/tree/main/room-worker): room projector · Room-side background processing for subscriptions, keys, and system-message style fanout. · `room-worker`
- [Data migration](https://github.com/hmchangw/chat/tree/main/data-migration): migration pipeline · 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](https://github.com/hmchangw/chat/tree/main/pkg): shared libs · Reusable infrastructure: NATS, errcodes, stores, room keys, at-rest encryption, search, and domain models. · `pkg`
- [Tools](https://github.com/hmchangw/chat/tree/main/tools): ops tooling · Local dev, observability, load generation, NATS debug, profiling, and sample-data seeding tools. · `tools`
- [Docker local](https://github.com/hmchangw/chat/tree/main/docker-local): dev stack · Local dependency and service compositions for development and migration demos. · `docker-local`

## Connections

- 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.
