How huypham12/clone-x-ver2 works
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: database.service.ts MongoDB domain store · app.ts Application bootstrap · notification-fanout.worker.ts Notification worker · media.service.ts Media service
Components 19
Node API process
Single deployment hosts HTTP, Socket.IO, and workers.
- Application bootstrap · Initializes dependencies, routes, sockets, and workers.
src/app.ts - HTTP API boundary · Module routes validate requests before service access.
src/modules/auth/auth.route.ts - Auth and error middleware · Applies authorization and normalized failures.
src/middleware/verify.middleware.ts - Health endpoints · Separates liveness from dependency readiness.
src/modules/health/health.service.ts - Socket.IO server · Uses an in-memory adapter and is limited to one API instance.
src/socket/socket-server.ts
Domain workflows
- Product services · Implements core product APIs and business writes.
src/modules/tweet/tweet.controller.ts - Conversation commands · Checks access, sends messages, extracts mentions, and updates read state.
src/modules/conversation/conversation-message-command.service.ts - Realtime delivery abstraction · Emits post-commit events to connected clients.
src/modules/realtime/in-process-realtime-emitter.ts - Transactional outbox · Atomically records domain events with business writes.
src/modules/events/outbox.repository.ts - Outbox publisher · Publishes recoverable outbox events to BullMQ.
src/modules/events/outbox.publisher.ts - Notification queues · Separates notification fan-out from notification work.
src/queues/notification-fanout.queue.ts - Notification worker · Applies eligibility, aggregation, lifecycle guards, and retries.
src/modules/notification/notification-fanout.worker.ts - Notification state · Durable notifications and unread counts support REST reconciliation.
src/modules/notification/notification-event.handler.ts - Media service · Stores durable media references and selects inline or queued processing.
src/modules/media/media.service.ts - Media processing queue · Receives optional asynchronous media work.
src/queues/media-processing.queue.ts - Media reconciler · Re-enqueues pending media records.
src/modules/media/media-processing.reconciler.ts
External infrastructure
- MongoDB domain store · Source of truth for domain state, read state, and transactional outbox.
src/config/database.service.ts - Redis · Backs BullMQ plus best-effort cache and presence.
src/config/redis.service.ts - Cloudinary · Durable media storage.
src/modules/media/cloudinary-media.storage.ts
Connections 27
- 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