Skip to content

Architecture

1. Bối cảnh hệ thống (C4 L1)

2. Góc nhìn Container (C4 L2)

3. Góc nhìn Component (C4 L3) — Phân lớp nội bộ

LớpTrách nhiệm
RoutesBề mặt HTTP trong RestPaths (/socket/websocket/clients, /notifications) + WS /stream
ControllersCổng auth + permission + phạm vi người nhận (userId từ JWT)
ServicesFaçade messaging WS, CRUD notification, xử lý message Kafka
Repositoriesre-export @nx/core; PolicyDefinitionRepository giải người nhận
ComponentsXuyên suốt: WebSocket server, Redis, ECDH, Kafka consumer

4. Chỉ mục máy trạng thái

Signal không có entity domain stateful sống lâu. Gần nhất là vòng đời kết nối WebSocket ngắn ngủi và trạng thái đã đọc (nhị phân) của notification.

EntityTrạng tháiSơ đồ
Kết nối WebSocketCONNECTING, AUTHENTICATED, ENCRYPTED, CLOSED→ nhảy tới
ActivityNotification.isReadUNREAD, READ→ nhảy tới

Kết nối WebSocket

TừSự kiệnĐếnĐiều kiện
CONNECTINGauthenticateAUTHENTICATEDtype=Bearer + JWT hợp lệ qua JWKSVerifierTokenService
AUTHENTICATEDhandshakeENCRYPTEDclientPublicKey → derive + lưu khoá AES
ENCRYPTEDdisconnectCLOSEDkhoá AES xoá khỏi map _clientAesKeys

Trạng thái đọc Notification

TừSự kiệnĐếnĐiều kiện
UNREADmark-readREADrecipientId phải khớp subject JWT; đặt isRead=true, readAt=now()

5. Kịch bản runtime

5.1 Kết nối WebSocket mã hoá

BướcChi tiết
2Trường payload là clientPublicKey; requireEncryption: true từ chối kết nối thiếu nó
5Cặp khoá ephemeral theo từng client → forward secrecy; khoá chỉ sống trong bộ nhớ

5.2 Pipeline Activity-Notification

BướcChi tiết
3orgfindUserIdsInOrganizer; merchantfindUserIdsInMerchant; usersrecipientIds rõ ràng; fallback [actorId]
8Commit theo từng message sau khi handler return; lỗi on-message được log, không retry

5.3 Cross-Service Push

BướcChi tiết
1Producer không host WS server — họ chỉ publish; signal là edge duy nhất
3sendToClient deliver cục bộ nếu có, không thì re-publish tới instance sở hữu

6. Mối quan tâm xuyên suốt

Mối quan tâmCách service này xử lý
AuthN (REST)Chiến lược JWT + Basic, xác minh qua JWKS từ xa (VerifierApplication)
AuthN (WS)Token type=Bearer xác minh bởi JWKSVerifierTokenService trong handler authenticate
AuthZPermission Casbin WebSocketClient.* trên route quản lý client; /notifications phạm vi người nhận (không permission rõ ràng, scope theo subject JWT)
EncryptionECDH P-256 + HKDF-SHA-256 + AES-256-GCM, khoá theo từng client trong map _clientAesKeys
i18nNội dung notification dựng phía server (hiện template tiếng Việt cho PAYMENT_SUCCESS)
Loggingkey-value có cấu trúc (key: %s); Kafka consumer log topic/partition/offset
IdempotencyKhông có ở consumer — at-least-once; re-delivery sẽ tạo hàng trùng (xem API Events)
Room ACLvalidateRoom hiện là passthrough (chấp nhận mọi room); ACL phạm vi merchant là TODO được ghi
IDsSnowflake qua IdGenerator, worker 9

7. Trang liên quan

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