Real-time authorization and flow
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Authorize a topic and recheck revocation
const grants=new Map([['u1',new Set(['room-a'])]]);function subscribe(user,room){return grants.get(user)?.has(room)===true;}console.log(subscribe('u1','room-a'),subscribe('u1','room-b'));grants.get('u1').delete('room-a');console.log(subscribe('u1','room-a'));Expected output:
true falsefalseThe access check rejects another room and a revoked membership.
How it works
Validate messages and verify permissions when a client subscribes or sends a command. Revoke active subscriptions when access changes; decide how to revalidate long-lived credentials. For Node HTTP responses, a false return from write signals that the producer should pause until drain or termination. With WebSockets, consult the specific library’s pending-byte and send completion contracts; browser-style bufferedAmount is not a universal server flow-control API. Choose an explicit policy: drop replaceable snapshots, disconnect slow consumers, or persist durable events for later replay. Multi-instance delivery needs a shared coordination mechanism; ordering and affinity depend on the protocol and deployment.
Bound a snapshot queue
const queue=[];function enqueue(snapshot){ if(queue.length===2) queue.shift(); queue.push(snapshot);}for(const value of [10,11,12])enqueue(value);console.log(JSON.stringify(queue));Expected output:
[11,12]The oldest replaceable snapshot is dropped. This policy must not be used for durable commands that cannot be lost.
Common mistakes
Do not trust a room name or subscription ID as authorization. Do not keep an unbounded array of pending updates. Do not wait forever for drain after a connection closes.
Practice and next steps
Design a two-tenant dashboard. Specify how access revocation affects an existing subscription and how a slow client can recover from dropped snapshot messages.
Further reading: https://nodejs.org/download/release/v22.16.0/docs/api/http.html. Related staged reference: node-real-time-authorization-and-flow-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Real-time authorization and flow — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
A user authenticated earlier but lost room access. What should happen?
What does response.write returning false mean?
When is dropping old queued snapshots reasonable?