Master Flow and Algorithms
This page contains the master flow diagram of the Micro Socket lifecycle and links to sub-algorithms.
Master Algorithm Flow
The diagram below shows the path of a message from publish to client delivery across all layers.
Sub-Algorithms
| Algorithm | Description | Detail page |
|---|---|---|
| Dedup / Idempotency | Prevent double processing via messageId | Dedup & Idempotency |
| ACL | SUBSCRIBE/PUBLISH authorization | ACL & Authorization |
| Ordering (SeqNo) | Message order and gap/replay | Ordering & SeqNo |
| Resume | Session and replay after disconnect | Resume & Replay |
Pseudocode: Gateway Main Loop (Summary)
FUNCTION handleMessage(ws, raw):
msg = JSON.parse(raw)
user = getUserId(ws.token)
IF msg.type == "subscribe":
IF NOT aclAllow(user, msg.topic, "read") THEN sendError(ws, "ACL_DENIED"); RETURN
addSubSocket(ws, msg.topic)
RETURN
IF msg.type == "publish":
IF NOT aclAllow(user, msg.topic, "write") THEN sendError(ws, "ACL_DENIED"); RETURN
IF dedupContains(user.id, msg.messageId) THEN sendAck(ws, msg.messageId, "duplicate"); RETURN
dedupAdd(user.id, msg.messageId)
seqNo = nextSeq(msg.topic)
publishToBackbone(msg.topic, { ...msg.payload, seqNo })
sendAck(ws, msg.messageId, "ok")
RETURN
IF msg.type == "resume":
session = getSession(msg.sessionId)
IF NOT session THEN sendError(ws, "SESSION_EXPIRED"); RETURN
replayGap(session, msg.lastSeqPerTopic, ws)
RETURNFull implementation in Node.js: Gateway (Overview), Gateway ACL + Dedup.