How the Emitter works in Node.js vs Web environments
masterThe emitter component allows you to subscribe to specific JSON tokens as events. While the conceptual model is the same (token-name as event name, token-value as event payload), the implementation differs based on the substrate:
Node.js Emitter
Extends Writable (an EventEmitter). Consumers subscribe using the standard .on(name, fn) pattern.
Web Emitter
Returns an EventTarget with a .writable WritableStream attached. Consumers subscribe using the standard .addEventListener(name, ev => ev.detail) pattern.
This distinction allows stream-json to work seamlessly across Node.js, Bun, Deno, and modern browsers without requiring polyfills.
/* Node.js subscription pattern */
const emitter = new Emitter();
emitter.on('token-name', (value) => {
console.log(value);
});
/* Web/Browser subscription pattern */
const emitter = new WebEmitter();
emitter.addEventListener('token-name', (ev) => {
console.log(ev.detail);
});