Dev Helpers
← Back

Deliver to multiple listeners

Deliver to multiple listeners using Node.js.

Open this example in the interactive library

Source code

import { channel, subscribe, unsubscribe } from "node:diagnostics_channel";
const name = "helpers:sample";
const ch = channel(name);
console.log(
  (() => {
    const rows = [];
    ch.subscribe(() => rows.push(1));
    ch.subscribe(() => rows.push(2));
    ch.publish(null);
    return rows;
  })(),
);

Sample output

[ 1, 2 ]