Manage application subscriptions and event ownership
Quick answer
Publish-subscribe connects producers with listeners through an explicit notification contract. It is useful when several components need to react to a domain change, but uncontrolled global event channels obscure dependencies and leak listeners.
How it works
A small in-process channel can use a Set of callbacks. subscribe returns an idempotent unsubscribe function. Publish can take a snapshot so subscription changes do not alter which callbacks are selected for this notification. Define error behavior: the example propagates the first thrown listener error and stops subsequent callbacks. It is synchronous, not a durable queue. Scope channels to the relevant feature and unsubscribe when owners dispose.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {const subs=new Set();const subscribe=fn=>{subs.add(fn);return ()=>subs.delete(fn);};const publish=v=>{for(const fn of [...subs])fn(v);};const out=[];const off=subscribe(v=>out.push(v));publish(2);off();off();publish(3);console.log(JSON.stringify(out),subs.size);})()Expected console output:
[2] 0The subscription receives only the first value and repeated cleanup is harmless.
Run in Node.js v22.16.0 as a CommonJS script. Actual ByteMind editor support is unverified.
(async () => {const seen=[],listeners=new Set();const second=()=>seen.push("B");listeners.add(()=>{seen.push("A");listeners.delete(second);});listeners.add(second);for(const f of [...listeners])f();for(const f of [...listeners])f();console.log(seen.join(","));})()Expected console output:
A,B,AThe first snapshot includes both listeners; the second includes only A.
Common mistakes
Do not assume a notification bus provides persistence, asynchronous isolation or exactly-once delivery. Avoid a global bus for ordinary direct calls.
Practice
Create a feature-scoped channel; subscribe two listeners, publish once, unsubscribe one twice, then publish again and inspect the received values.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Manage application subscriptions and event ownership — Knowledge Check
Apply the concept and reason about local example output.
What should a feature subscription return to its owner?
What does snapshotting listeners define?
When is a direct function call often clearer than a bus?
What is the exact console output in Manage application subscriptions and event ownership example 1?
(async () => {const subs=new Set();const subscribe=fn=>{subs.add(fn);return ()=>subs.delete(fn);};const publish=v=>{for(const fn of [...subs])fn(v);};const out=[];const off=subscribe(v=>out.push(v));publish(2);off();off();publish(3);console.log(JSON.stringify(out),subs.size);})()What is the exact console output in Manage application subscriptions and event ownership example 2?
(async () => {const seen=[],listeners=new Set();const second=()=>seen.push("B");listeners.add(()=>{seen.push("A");listeners.delete(second);});listeners.add(second);for(const f of [...listeners])f();for(const f of [...listeners])f();console.log(seen.join(","));})()