# huypham12/clone-x-ver2 architecture

> Architecture diagram of the GitHub repository huypham12/clone-x-ver2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/huypham12/clone-x-ver2
- Repository: https://github.com/huypham12/clone-x-ver2

## Overview

#### Purpose and Runtime Entry Points
This is a TypeScript/Node.js backend for an X/Twitter-like portfolio application: REST API, Socket.IO chat/realtime, media handling, social features, and notification delivery. `src/app.ts` is the application bootstrap; it connects dependencies, initializes workers/routes/socket support, and starts the single API process. HTTP routes are organized per module, while `src/socket/socket-server.ts` and `src/socket/chat.handler.ts` handle Socket.IO.

#### API Composition and Request Boundary
Controllers, validators, and routes form the HTTP boundary; middleware applies authentication/authorization and normalized failures before services access data. Core examples:
- `src/modules/auth/auth.route.ts`
- `src/modules/tweet/tweet.controller.ts`
- `src/middleware/verify.middleware.ts`
- `src/middleware/error-handler.middleware.ts`

Auth supports registration/login, JWT access/refresh token flows, and OAuth-oriented service logic. User, tweet, search, and conversation modules expose the main product APIs.

#### Persistent Domain Model
MongoDB is the source of truth and must support multi-document transactions. Mongoose schemas model users, tweets, follows, conversations/messages, media, notifications, refresh tokens, and the transactional outbox:
- `src/config/database.service.ts`
- `src/schemas/User.schema.ts`
- `src/schemas/Message.schema.ts`
- `src/schemas/OutboxEvent.schema.ts`

Business writes can atomically persist domain changes and an outbox event. Read-state schemas provide durable reconciliation for notification and conversation unread counts rather than relying on realtime delivery.

#### Conversations and Realtime Delivery
Conversation logic is decomposed into access checks, message commands, mention extraction, delivery, hydration, synchronization, and read-state updates:
- `src/modules/conversation/conversation-message-command.service.ts`
- `src/modules/conversation/conversation-message-delivery.service.ts`
- `src/modules/conversation/conversation-read.service.ts`

Socket.IO runs in the same process with the in-memory adapter. Realtime emission is therefore restricted to one API instance; clients must reconcile via REST after reconnect. `src/modules/realtime/in-process-realtime-emitter.ts` is the delivery abstraction.

#### Transactional Events and Notifications
Domain events bridge business actions to eventual notification processing. Events enter a MongoDB outbox, are published to BullMQ, then processed under notification policy and lifecycle controls:
- `src/modules/events/outbox.repository.ts`
- `src/modules/events/outbox.publisher.ts`
- `src/modules/notification/notification-event.handler.ts`
- `src/modules/notification/notification-fanout.worker.ts`

Notification persistence, aggregation, cursor queries, eligibility/policy decisions, unread state, and post-commit realtime delivery are separated. Lifecycle guards help prevent invalid or duplicate effects; the system is retry-oriented, not globally exactly-once.

#### Queues, Redis, and Media Pipeline
Redis backs BullMQ and best-effort cache/presence capabilities; MongoDB/outbox remains the recovery source. Queue definitions isolate notification fan-out, notification work, and media processing:
- `src/queues/notification-fanout.queue.ts`
- `src/queues/media-processing.queue.ts`
- `src/config/redis.service.ts`

Media uploads use Cloudinary as durable object storage. In portfolio mode, processing is inline; optional queued mode stores durable media references and uses reconciliation to re-enqueue pending records:
- `src/modules/media/media.service.ts`
- `src/modules/media/cloudinary-media.storage.ts`
- `src/modules/media/media-processing.reconciler.ts`

#### Deployment and Operational Boundaries
The intended deployment is one Dockerized Node 22 process containing HTTP, Socket.IO, and workers. MongoDB, Redis TCP, and Cloudinary are required external services. Docker, Compose, and Render definitions establish this runtime contract:
- `Dockerfile`
- `docker-compose.yml`
- `render.yaml`

Health endpoints are implemented separately from feature routing and distinguish liveness from dependency readiness:
- `src/modules/health/health.route.ts`
- `src/modules/health/health.service.ts`

Main entry points: [`src/config/database.service.ts`](https://github.com/huypham12/clone-x-ver2/blob/main/src/config/database.service.ts) (MongoDB domain store), [`src/app.ts`](https://github.com/huypham12/clone-x-ver2/blob/main/src/app.ts) (Application bootstrap), [`src/modules/notification/notification-fanout.worker.ts`](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/notification/notification-fanout.worker.ts) (Notification worker), [`src/modules/media/media.service.ts`](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/media.service.ts) (Media service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Node API process"]
  node_bootstrap["Application bootstrap<br/>Node entrypoint<br/>[app.ts]"]
  node_http["HTTP API boundary<br/>routes/controllers<br/>[auth.route.ts]"]
  node_middleware["Auth and error middleware<br/>HTTP middleware"]
  node_health["Health endpoints<br/>health service<br/>[health.service.ts]"]
  node_socket["Socket.IO server<br/>realtime transport<br/>[socket-server.ts]"]
end

subgraph group_domain["Domain workflows"]
  node_domain_services["Product services<br/>auth/user/tweet/search"]
  node_conversation["Conversation commands<br/>message workflow"]
  node_realtime_emitter["Realtime delivery abstraction<br/>in-process emitter"]
  node_outbox["Transactional outbox<br/>event repository"]
  node_outbox_publisher["Outbox publisher<br/>event publisher"]
  node_notification_queue["Notification queues<br/>BullMQ fan-out/work"]
  node_notification_worker["Notification worker<br/>policy and lifecycle worker"]
  node_notification_state["Notification state<br/>notifications and unread state"]
  node_media_service["Media service<br/>upload orchestration<br/>[media.service.ts]"]
  node_media_queue["Media processing queue<br/>BullMQ queue"]
  node_media_reconciler["Media reconciler<br/>recovery worker"]
end

subgraph group_infra["External infrastructure"]
  node_mongo[("MongoDB domain store<br/>Mongoose database")]
  node_redis[("Redis<br/>queue/cache/presence<br/>[redis.service.ts]")]
  node_cloudinary[("Cloudinary<br/>object storage")]
end

node_bootstrap -->|"mounts"| node_http
node_bootstrap -->|"starts"| node_socket
node_bootstrap -->|"initializes"| node_outbox_publisher
node_bootstrap -->|"runs"| node_notification_worker
node_http -->|"passes through"| node_middleware
node_middleware -->|"authorizes"| node_domain_services
node_http -->|"serves"| node_health
node_domain_services -->|"reads and writes"| node_mongo
node_conversation -->|"persists messages and read state"| node_mongo
node_socket -->|"chat commands"| node_conversation
node_conversation -->|"delivers after commit"| node_realtime_emitter
node_realtime_emitter -->|"emits to clients"| node_socket
node_domain_services -->|"writes atomically"| node_outbox
node_outbox -->|"stored with transaction"| node_mongo
node_outbox_publisher -->|"claims pending events"| node_outbox
node_outbox_publisher -->|"publishes jobs"| node_notification_queue
node_notification_queue -->|"backed by"| node_redis
node_notification_worker -->|"consumes"| node_notification_queue
node_notification_worker -->|"persists effects"| node_notification_state
node_notification_state -->|"stored in"| node_mongo
node_notification_worker -->|"post-commit notification"| node_realtime_emitter
node_media_service -->|"uploads media"| node_cloudinary
node_media_service -->|"stores media reference"| node_mongo
node_media_service -.->|"optional queued processing"| node_media_queue
node_media_queue -->|"backed by"| node_redis
node_media_reconciler -->|"finds pending media"| node_mongo
node_media_reconciler -.->|"re-enqueues"| node_media_queue

click node_bootstrap "https://github.com/huypham12/clone-x-ver2/blob/main/src/app.ts"
click node_http "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/auth/auth.route.ts"
click node_middleware "https://github.com/huypham12/clone-x-ver2/blob/main/src/middleware/verify.middleware.ts"
click node_health "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/health/health.service.ts"
click node_socket "https://github.com/huypham12/clone-x-ver2/blob/main/src/socket/socket-server.ts"
click node_domain_services "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/tweet/tweet.controller.ts"
click node_conversation "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/conversation/conversation-message-command.service.ts"
click node_realtime_emitter "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/realtime/in-process-realtime-emitter.ts"
click node_mongo "https://github.com/huypham12/clone-x-ver2/blob/main/src/config/database.service.ts"
click node_outbox "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/events/outbox.repository.ts"
click node_outbox_publisher "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/events/outbox.publisher.ts"
click node_redis "https://github.com/huypham12/clone-x-ver2/blob/main/src/config/redis.service.ts"
click node_notification_queue "https://github.com/huypham12/clone-x-ver2/blob/main/src/queues/notification-fanout.queue.ts"
click node_notification_worker "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/notification/notification-fanout.worker.ts"
click node_notification_state "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/notification/notification-event.handler.ts"
click node_media_service "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/media.service.ts"
click node_media_queue "https://github.com/huypham12/clone-x-ver2/blob/main/src/queues/media-processing.queue.ts"
click node_media_reconciler "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/media-processing.reconciler.ts"
click node_cloudinary "https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/cloudinary-media.storage.ts"

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_bootstrap,node_http,node_middleware,node_health,node_socket toneBlue
class node_domain_services,node_conversation,node_realtime_emitter,node_outbox,node_outbox_publisher,node_notification_queue,node_notification_worker,node_notification_state,node_media_service,node_media_queue,node_media_reconciler toneAmber
class node_mongo,node_redis,node_cloudinary toneMint
```

## Components

### Node API process

Single deployment hosts HTTP, Socket.IO, and workers.

- [Application bootstrap](https://github.com/huypham12/clone-x-ver2/blob/main/src/app.ts): Node entrypoint · Initializes dependencies, routes, sockets, and workers. · `src/app.ts`
- [HTTP API boundary](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/auth/auth.route.ts): routes/controllers · Module routes validate requests before service access. · `src/modules/auth/auth.route.ts`
- [Auth and error middleware](https://github.com/huypham12/clone-x-ver2/blob/main/src/middleware/verify.middleware.ts): HTTP middleware · Applies authorization and normalized failures. · `src/middleware/verify.middleware.ts`
- [Health endpoints](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/health/health.service.ts): health service · Separates liveness from dependency readiness. · `src/modules/health/health.service.ts`
- [Socket.IO server](https://github.com/huypham12/clone-x-ver2/blob/main/src/socket/socket-server.ts): realtime transport · Uses an in-memory adapter and is limited to one API instance. · `src/socket/socket-server.ts`

### Domain workflows

- [Product services](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/tweet/tweet.controller.ts): auth/user/tweet/search · Implements core product APIs and business writes. · `src/modules/tweet/tweet.controller.ts`
- [Conversation commands](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/conversation/conversation-message-command.service.ts): message workflow · Checks access, sends messages, extracts mentions, and updates read state. · `src/modules/conversation/conversation-message-command.service.ts`
- [Realtime delivery abstraction](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/realtime/in-process-realtime-emitter.ts): in-process emitter · Emits post-commit events to connected clients. · `src/modules/realtime/in-process-realtime-emitter.ts`
- [Transactional outbox](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/events/outbox.repository.ts): event repository · Atomically records domain events with business writes. · `src/modules/events/outbox.repository.ts`
- [Outbox publisher](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/events/outbox.publisher.ts): event publisher · Publishes recoverable outbox events to BullMQ. · `src/modules/events/outbox.publisher.ts`
- [Notification queues](https://github.com/huypham12/clone-x-ver2/blob/main/src/queues/notification-fanout.queue.ts): BullMQ fan-out/work · Separates notification fan-out from notification work. · `src/queues/notification-fanout.queue.ts`
- [Notification worker](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/notification/notification-fanout.worker.ts): policy and lifecycle worker · Applies eligibility, aggregation, lifecycle guards, and retries. · `src/modules/notification/notification-fanout.worker.ts`
- [Notification state](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/notification/notification-event.handler.ts): notifications and unread state · Durable notifications and unread counts support REST reconciliation. · `src/modules/notification/notification-event.handler.ts`
- [Media service](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/media.service.ts): upload orchestration · Stores durable media references and selects inline or queued processing. · `src/modules/media/media.service.ts`
- [Media processing queue](https://github.com/huypham12/clone-x-ver2/blob/main/src/queues/media-processing.queue.ts): BullMQ queue · Receives optional asynchronous media work. · `src/queues/media-processing.queue.ts`
- [Media reconciler](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/media-processing.reconciler.ts): recovery worker · Re-enqueues pending media records. · `src/modules/media/media-processing.reconciler.ts`

### External infrastructure

- [MongoDB domain store](https://github.com/huypham12/clone-x-ver2/blob/main/src/config/database.service.ts): Mongoose database · Source of truth for domain state, read state, and transactional outbox. · `src/config/database.service.ts`
- [Redis](https://github.com/huypham12/clone-x-ver2/blob/main/src/config/redis.service.ts): queue/cache/presence · Backs BullMQ plus best-effort cache and presence. · `src/config/redis.service.ts`
- [Cloudinary](https://github.com/huypham12/clone-x-ver2/blob/main/src/modules/media/cloudinary-media.storage.ts): object storage · Durable media storage. · `src/modules/media/cloudinary-media.storage.ts`

## Connections

- Application bootstrap → HTTP API boundary: mounts
- Application bootstrap → Socket.IO server: starts
- Application bootstrap → Outbox publisher: initializes
- Application bootstrap → Notification worker: runs
- HTTP API boundary → Auth and error middleware: passes through
- Auth and error middleware → Product services: authorizes
- HTTP API boundary → Health endpoints: serves
- Product services → MongoDB domain store: reads and writes
- Conversation commands → MongoDB domain store: persists messages and read state
- Socket.IO server → Conversation commands: chat commands
- Conversation commands → Realtime delivery abstraction: delivers after commit
- Realtime delivery abstraction → Socket.IO server: emits to clients
- Product services → Transactional outbox: writes atomically
- Transactional outbox → MongoDB domain store: stored with transaction
- Outbox publisher → Transactional outbox: claims pending events
- Outbox publisher → Notification queues: publishes jobs
- Notification queues → Redis: backed by
- Notification worker → Notification queues: consumes
- Notification worker → Notification state: persists effects
- Notification state → MongoDB domain store: stored in
- Notification worker → Realtime delivery abstraction: post-commit notification
- Media service → Cloudinary: uploads media
- Media service → MongoDB domain store: stores media reference
- Media service → Media processing queue: optional queued processing
- Media processing queue → Redis: backed by
- Media reconciler → MongoDB domain store: finds pending media
- Media reconciler → Media processing queue: re-enqueues
