Skip to content

Signal Service

@nx/signal là edge real-time duy nhất của nền tảng: một WebSocket server mã hoá đầu-cuối (/stream) mà các service khác push tới qua Redis pub/sub, cộng một pipeline activity-notification do Kafka cấp giải người nhận, lưu hàng ActivityNotification (trong @nx/core), và push chúng tới room theo từng người nhận. Nó không sở hữu schema riêng — chỉ seed migration cho permission của nó.

1. Tham chiếu nhanh

Thuộc tínhGiá trị
Package@nx/signal
CodeSVC-00090-SIGNAL
TypeMicroservice
RuntimeBun
Base ClassVerifierApplication
Locationpackages/signal
Base Path/v1/api/signal
WebSocket Path/stream (requireEncryption: true)
Dev Port31090
Container Port3000 (ngoài 31090)
Snowflake ID9
DB Schemakhông có riêng — tái dùng bảng @nx/core (ActivityNotification)
Binding Namespace@nx/signal

2. Mục đích & Phạm vi

Bao gồmLoại trừ
WebSocket server mã hoá (/stream, ECDH P-256 + AES-256-GCM)Lưu kết nối WebSocket (in-memory theo từng instance, mất khi restart)
Fan-out xuyên instance / xuyên service qua Redis pub/subSản xuất sự kiện domain (signal chỉ consume + push)
REST quản lý client (list, get, broadcast, room/client send, disconnect)Soạn nội dung notification cho loại sự kiện tuỳ ý (hiện chỉ PAYMENT_SUCCESS)
Pipeline activity-notification Kafka (resolve → persist → push)Sở hữu schema ActivityNotification (tập trung trong @nx/core)
REST notification phạm vi người nhận (list, count, mark-read)Kênh Email / SMS / push-notification
Xác minh JWT qua JWKS từ xa (Identity)Phát hành token (uỷ thác cho @nx/identity)

3. Tech Stack

External:

LibraryMục đích
@venizia/ignisIoC container, DI, BaseComponent, WebSocketComponent, JWKSVerifierTokenService
@venizia/ignis-helpersECDH, WebSocketServerHelper, WebSocketEmitter, KafkaConsumerHelper
@platformatic/kafka(De)serializer Kafka + types SASL cho notification consumer
honoFramework HTTP server (qua IGNIS)
@hono/zod-openapiSinh OpenAPI từ Zod schema
zodValidate schema request/response
drizzle-orm / pgTruy cập DB qua PostgresCoreDataSource (lưu notification + authz)

Internal:

PackageMục đích
@nx/coreVerifierApplication, createAppConfig, bootstrapApplication, repository/model tập trung (ActivityNotification), KafkaTopics, WebSocketTopics/WebSocketRooms, RedisConnectionFactory, BaseSocketEventService

4. Cấu trúc dự án

packages/signal/
├── src/
│   ├── application.ts                  # subclass VerifierApplication
│   ├── index.ts                        # bootstrapApplication()
│   ├── migrate.ts                      # bootstrapMigration()
│   ├── common/                         # BindingKeys, RestPaths, WebSocketClientInfoSchema
│   ├── components/
│   │   ├── signal.component.ts         # Component orchestrator cấp cao
│   │   ├── websocket.component.ts      # NxWebSocketComponent (ECDH + Redis + handlers)
│   │   └── notification/               # NotificationKafkaComponent + socket-event service
│   ├── controllers/
│   │   ├── websocket-clients/          # WebSocketClientController (+ permissions)
│   │   └── activity-notifications/     # ActivityNotificationController
│   ├── datasources/                    # binding PostgresCoreDataSource
│   ├── errors/                         # WorkerErrors
│   ├── migrations/processes/           # 2 seed migration (permissions, role-permissions)
│   ├── models/requests/                # zod query schema
│   ├── services/                       # SignalEventService, ActivityNotification(Worker)Service
│   └── utilities/                      # content-decorator (markdown → content/html)
├── package.json
└── tsconfig.json

5. Kiến trúc

Chi tiết: xem Architecture.

6. Ảnh chụp Domain

Signal không sở hữu bảng nào. Nó đọc/ghi bảng tập trung ActivityNotification từ @nx/core.

ERD đầy đủ + bảng entity: xem Domain Model.

7. Tổng quan bề mặt

REST controllers — tham chiếu đầy đủ render trực tiếp từ /v1/api/signal/doc/openapi.json (Scalar viewer tại /doc, portal gateway):

ControllerBase pathGhi chú
WebSocketClientController/socket/websocket/clientsstatus (public) + list / get / broadcast / room-send / client-send / disconnect (auth + permission WebSocketClient.*)
ActivityNotificationController/notificationsJWT, phạm vi người nhận: list / count / mark-read / mark-all-read

Bề mặt async — tham chiếu đầy đủ trong API Events:

HướngKênhSố lượng
InboundKafka1 (SIGNAL_ACTIVITY_NOTIFICATION)
OutboundKafka0
OutboundWebSocket1 topic (observation/signal/notification/created) + broadcast/room/client send tuỳ biến

8. Components

ComponentFileMục đích
SignalComponentsrc/components/signal.component.tsOrchestrator — đăng ký SignalEventService + ActivityNotificationService, WebSocketClientController + ActivityNotificationController, và hai sub-component bên dưới
NxWebSocketComponentsrc/components/websocket.component.tsRedis, mã hoá ECDH, options /stream, 6 binding handler, IGNIS WebSocketComponent, WebSocketEmitter
NotificationKafkaComponentsrc/components/notification/component.tsBind notification service, khởi động consumer SIGNAL_ACTIVITY_NOTIFICATION (autocommit off, fallbackMode: latest, shutdown graceful)

9. Services

ServiceFileMột dòng
SignalEventServiceservices/signal-event.service.tsFaçade trên WS server/emitter (broadcast, room/client send, introspection, disconnect)
ActivityNotificationServiceservices/activity-notification.service.tsRead/update phạm vi người nhận trên ActivityNotificationRepository (list, count, mark-read, mark-all)
ActivityNotificationWorkerServiceservices/activity-notification-worker.service.tsHandler Kafka — giải người nhận, dựng nội dung, lưu hàng, push WS
NotificationSocketEventServicecomponents/notification/socket-event.service.tsSubclass BaseSocketEventService; emit tới room theo từng người nhận

10. Repositories

Tất cả re-export từ @nx/core; signal không sở hữu schema. Đăng ký trong preConfigure() (+ rebind bởi NotificationKafkaComponent).

RepositoryTableSourceMethod tuỳ chỉnh
ActivityNotificationRepositoryActivityNotification@nx/core— (SoftDeletableRepository thuần)
PolicyDefinitionRepositoryPolicyDefinition@nx/corefindUserIdsInOrganizer, findUserIdsInMerchant (giải người nhận)
PermissionRepositoryPermission@nx/core— (seed migration)
RoleRepositoryRole@nx/core— (seed migration)

11. Entry Points

FileMục đích
src/index.tsEntry service → bootstrapApplication()
src/migrate.tsEntry migration → bootstrapMigration() (seed permissions + role-permissions)
src/application.tsApplication extends VerifierApplication

12. Configuration

Env vars + dữ liệu seed: xem Configuration.

13. Operations

Triển khai + observability + bảo mật + runbook: xem Operations.

14. Trang liên quan

Khái niệm — vì sao/như thế nào:

Tham chiếu — tra cứu:

Tính năng — đào sâu:

Decisions:

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.