Diagnostics

diagnostics_channel.unsubscribe()

Removes a previously registered subscriber using the same function identity. It returns whether a registration was removed. Use this when a test or instrumentation owner ends its lifetime. Tested on Node.js 22.16.0. Example output (one line per entry): ["true", "1"].

Formal Syntax & Structure

nodejs syntaxnodejs
diagnostics_channel.unsubscribe(name, onMessage)

Parameters & Attributes

NameTypeDescription
namestring | symbolChannel name.
onMessageFunctionOriginal handler.

Return Value

boolean

Whether a subscription was removed.

Working Example

Example Snippetnodejs
import { channel, subscribe, unsubscribe } from 'node:diagnostics_channel';const name = 'bytemind.v16.unsub';let count = 0;const handler = () => count++;subscribe(name, handler);channel(name).publish({});console.log(unsubscribe(name, handler));channel(name).publish({});console.log(count);
Node.js Reference