Skip to content

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

AlgorithmDescriptionDetail page
Dedup / IdempotencyPrevent double processing via messageIdDedup & Idempotency
ACLSUBSCRIBE/PUBLISH authorizationACL & Authorization
Ordering (SeqNo)Message order and gap/replayOrdering & SeqNo
ResumeSession and replay after disconnectResume & 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)
    RETURN

Full implementation in Node.js: Gateway (Overview), Gateway ACL + Dedup.

Star the repo on GitHub if this documentation is useful — link in the navbar above.