/usr/share/doc/nodejs/html/api
# Diagnostics Channel <!--introduced_in=v15.1.0--> > Stability: 1 - Experimental <!-- source_link=lib/diagnostics_channel.js --> The `node:diagnostics_channel` module provides an API to create named channels to report arbitrary message data for diagnostics purposes. It can be accessed using: ```mjs import diagnostics_channel from 'node:diagnostics_channel'; ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); ``` It is intended that a module writer wanting to report diagnostics messages will create one or many top-level channels to report messages through. Channels may also be acquired at runtime but it is not encouraged due to the additional overhead of doing so. Channels may be exported for convenience, but as long as the name is known it can be acquired anywhere. If you intend for your module to produce diagnostics data for others to consume it is recommended that you include documentation of what named channels are used along with the shape of the message data. Channel names should generally include the module name to avoid collisions with data from other modules. ## Public API ### Overview Following is a simple overview of the public API. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; // Get a reusable channel object const channel = diagnostics_channel.channel('my-channel'); function onMessage(message, name) { // Received data } // Subscribe to the channel diagnostics_channel.subscribe('my-channel', onMessage); // Check if the channel has an active subscriber if (channel.hasSubscribers) { // Publish data to the channel channel.publish({ some: 'data' }); } // Unsubscribe from the channel diagnostics_channel.unsubscribe('my-channel', onMessage); ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); // Get a reusable channel object const channel = diagnostics_channel.channel('my-channel'); function onMessage(message, name) { // Received data } // Subscribe to the channel diagnostics_channel.subscribe('my-channel', onMessage); // Check if the channel has an active subscriber if (channel.hasSubscribers) { // Publish data to the channel channel.publish({ some: 'data' }); } // Unsubscribe from the channel diagnostics_channel.unsubscribe('my-channel', onMessage); ``` #### `diagnostics_channel.hasSubscribers(name)` <!-- YAML added: - v15.1.0 - v14.17.0 --> * `name` {string|symbol} The channel name * Returns: {boolean} If there are active subscribers Check if there are active subscribers to the named channel. This is helpful if the message you want to send might be expensive to prepare. This API is optional but helpful when trying to publish messages from very performance-sensitive code. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; if (diagnostics_channel.hasSubscribers('my-channel')) { // There are subscribers, prepare and publish message } ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); if (diagnostics_channel.hasSubscribers('my-channel')) { // There are subscribers, prepare and publish message } ``` #### `diagnostics_channel.channel(name)` <!-- YAML added: - v15.1.0 - v14.17.0 --> * `name` {string|symbol} The channel name * Returns: {Channel} The named channel object This is the primary entry-point for anyone wanting to publish to a named channel. It produces a channel object which is optimized to reduce overhead at publish time as much as possible. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; const channel = diagnostics_channel.channel('my-channel'); ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); ``` #### `diagnostics_channel.subscribe(name, onMessage)` <!-- YAML added: - v16.17.0 --> * `name` {string|symbol} The channel name * `onMessage` {Function} The handler to receive channel messages * `message` {any} The message data * `name` {string|symbol} The name of the channel Register a message handler to subscribe to this channel. This message handler will be run synchronously whenever a message is published to the channel. Any errors thrown in the message handler will trigger an [`'uncaughtException'`][]. ```mjs import diagnostics_channel from 'diagnostics_channel'; diagnostics_channel.subscribe('my-channel', (message, name) => { // Received data }); ``` ```cjs const diagnostics_channel = require('diagnostics_channel'); diagnostics_channel.subscribe('my-channel', (message, name) => { // Received data }); ``` #### `diagnostics_channel.unsubscribe(name, onMessage)` <!-- YAML added: - v16.17.0 --> * `name` {string|symbol} The channel name * `onMessage` {Function} The previous subscribed handler to remove * Returns: {boolean} `true` if the handler was found, `false` otherwise. Remove a message handler previously registered to this channel with [`diagnostics_channel.subscribe(name, onMessage)`][]. ```mjs import diagnostics_channel from 'diagnostics_channel'; function onMessage(message, name) { // Received data } diagnostics_channel.subscribe('my-channel', onMessage); diagnostics_channel.unsubscribe('my-channel', onMessage); ``` ```cjs const diagnostics_channel = require('diagnostics_channel'); function onMessage(message, name) { // Received data } diagnostics_channel.subscribe('my-channel', onMessage); diagnostics_channel.unsubscribe('my-channel', onMessage); ``` ### Class: `Channel` <!-- YAML added: - v15.1.0 - v14.17.0 --> The class `Channel` represents an individual named channel within the data pipeline. It is used to track subscribers and to publish messages when there are subscribers present. It exists as a separate object to avoid channel lookups at publish time, enabling very fast publish speeds and allowing for heavy use while incurring very minimal cost. Channels are created with [`diagnostics_channel.channel(name)`][], constructing a channel directly with `new Channel(name)` is not supported. #### `channel.hasSubscribers` <!-- YAML added: - v15.1.0 - v14.17.0 --> * Returns: {boolean} If there are active subscribers Check if there are active subscribers to this channel. This is helpful if the message you want to send might be expensive to prepare. This API is optional but helpful when trying to publish messages from very performance-sensitive code. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; const channel = diagnostics_channel.channel('my-channel'); if (channel.hasSubscribers) { // There are subscribers, prepare and publish message } ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); if (channel.hasSubscribers) { // There are subscribers, prepare and publish message } ``` #### `channel.publish(message)` <!-- YAML added: - v15.1.0 - v14.17.0 --> * `message` {any} The message to send to the channel subscribers Publish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; const channel = diagnostics_channel.channel('my-channel'); channel.publish({ some: 'message' }); ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); channel.publish({ some: 'message' }); ``` #### `channel.subscribe(onMessage)` <!-- YAML added: - v15.1.0 - v14.17.0 deprecated: v16.17.0 --> > Stability: 0 - Deprecated: Use [`diagnostics_channel.subscribe(name, onMessage)`][] * `onMessage` {Function} The handler to receive channel messages * `message` {any} The message data * `name` {string|symbol} The name of the channel Register a message handler to subscribe to this channel. This message handler will be run synchronously whenever a message is published to the channel. Any errors thrown in the message handler will trigger an [`'uncaughtException'`][]. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; const channel = diagnostics_channel.channel('my-channel'); channel.subscribe((message, name) => { // Received data }); ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); channel.subscribe((message, name) => { // Received data }); ``` #### `channel.unsubscribe(onMessage)` <!-- YAML added: - v15.1.0 - v14.17.0 deprecated: v16.17.0 changes: - version: v16.14.0 pr-url: https://github.com/nodejs/node/pull/40433 description: Added return value. Added to channels without subscribers. --> > Stability: 0 - Deprecated: Use [`diagnostics_channel.unsubscribe(name, onMessage)`][] * `onMessage` {Function} The previous subscribed handler to remove * Returns: {boolean} `true` if the handler was found, `false` otherwise. Remove a message handler previously registered to this channel with [`channel.subscribe(onMessage)`][]. ```mjs import diagnostics_channel from 'node:diagnostics_channel'; const channel = diagnostics_channel.channel('my-channel'); function onMessage(message, name) { // Received data } channel.subscribe(onMessage); channel.unsubscribe(onMessage); ``` ```cjs const diagnostics_channel = require('node:diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); function onMessage(message, name) { // Received data } channel.subscribe(onMessage); channel.unsubscribe(onMessage); ``` ### Built-in Channels #### HTTP `http.client.request.start` * `request` {http.ClientRequest} Emitted when client starts a request. `http.client.response.finish` * `request` {http.ClientRequest} * `response` {http.IncomingMessage} Emitted when client receives a response. `http.server.request.start` * `request` {http.IncomingMessage} * `response` {http.ServerResponse} * `socket` {net.Socket} * `server` {http.Server} Emitted when server receives a request. `http.server.response.finish` * `request` {http.IncomingMessage} * `response` {http.ServerResponse} * `socket` {net.Socket} * `server` {http.Server} Emitted when server sends a response. #### NET `net.client.socket` * `socket` {net.Socket} Emitted when a new TCP or pipe client socket is created. `net.server.socket` * `socket` {net.Socket} Emitted when a new TCP or pipe connection is received. #### UDP `udp.socket` * `socket` {dgram.Socket} Emitted when a new UDP socket is created. #### Process <!-- YAML added: v16.18.0 --> `child_process` * `process` {ChildProcess} Emitted when a new process is created. #### Worker Thread <!-- YAML added: v16.18.0 --> `worker_threads` * `worker` [`Worker`][] Emitted when a new thread is created. [`'uncaughtException'`]: process.md#event-uncaughtexception [`Worker`]: worker_threads.md#class-worker [`channel.subscribe(onMessage)`]: #channelsubscribeonmessage [`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname [`diagnostics_channel.subscribe(name, onMessage)`]: #diagnostics_channelsubscribename-onmessage [`diagnostics_channel.unsubscribe(name, onMessage)`]: #diagnostics_channelunsubscribename-onmessage
.
Edit
..
Edit
addons.html
Edit
addons.json
Edit
addons.md
Edit
all.html
Edit
all.json
Edit
assert.html
Edit
assert.json
Edit
assert.md
Edit
assets
Edit
async_context.html
Edit
async_context.json
Edit
async_context.md
Edit
async_hooks.html
Edit
async_hooks.json
Edit
async_hooks.md
Edit
buffer.html
Edit
buffer.json
Edit
buffer.md
Edit
child_process.html
Edit
child_process.json
Edit
child_process.md
Edit
cli.html
Edit
cli.json
Edit
cli.md
Edit
cluster.html
Edit
cluster.json
Edit
cluster.md
Edit
console.html
Edit
console.json
Edit
console.md
Edit
corepack.html
Edit
corepack.json
Edit
corepack.md
Edit
crypto.html
Edit
crypto.json
Edit
crypto.md
Edit
debugger.html
Edit
debugger.json
Edit
debugger.md
Edit
deprecations.html
Edit
deprecations.json
Edit
deprecations.md
Edit
dgram.html
Edit
dgram.json
Edit
dgram.md
Edit
diagnostics_channel.html
Edit
diagnostics_channel.json
Edit
diagnostics_channel.md
Edit
dns.html
Edit
dns.json
Edit
dns.md
Edit
documentation.html
Edit
documentation.json
Edit
documentation.md
Edit
domain.html
Edit
domain.json
Edit
domain.md
Edit
embedding.html
Edit
embedding.json
Edit
embedding.md
Edit
errors.html
Edit
errors.json
Edit
errors.md
Edit
esm.html
Edit
esm.json
Edit
esm.md
Edit
events.html
Edit
events.json
Edit
events.md
Edit
fs.html
Edit
fs.json
Edit
fs.md
Edit
globals.html
Edit
globals.json
Edit
globals.md
Edit
http.html
Edit
http.json
Edit
http.md
Edit
http2.html
Edit
http2.json
Edit
http2.md
Edit
https.html
Edit
https.json
Edit
https.md
Edit
index.html
Edit
index.json
Edit
index.md
Edit
inspector.html
Edit
inspector.json
Edit
inspector.md
Edit
intl.html
Edit
intl.json
Edit
intl.md
Edit
module.html
Edit
module.json
Edit
module.md
Edit
modules.html
Edit
modules.json
Edit
modules.md
Edit
n-api.html
Edit
n-api.json
Edit
n-api.md
Edit
net.html
Edit
net.json
Edit
net.md
Edit
os.html
Edit
os.json
Edit
os.md
Edit
packages.html
Edit
packages.json
Edit
packages.md
Edit
path.html
Edit
path.json
Edit
path.md
Edit
perf_hooks.html
Edit
perf_hooks.json
Edit
perf_hooks.md
Edit
permissions.html
Edit
permissions.json
Edit
permissions.md
Edit
policy.html
Edit
policy.json
Edit
policy.md
Edit
process.html
Edit
process.json
Edit
process.md
Edit
punycode.html
Edit
punycode.json
Edit
punycode.md
Edit
querystring.html
Edit
querystring.json
Edit
querystring.md
Edit
readline.html
Edit
readline.json
Edit
readline.md
Edit
repl.html
Edit
repl.json
Edit
repl.md
Edit
report.html
Edit
report.json
Edit
report.md
Edit
stream.html
Edit
stream.json
Edit
stream.md
Edit
string_decoder.html
Edit
string_decoder.json
Edit
string_decoder.md
Edit
synopsis.html
Edit
synopsis.json
Edit
synopsis.md
Edit
test.html
Edit
test.json
Edit
test.md
Edit
timers.html
Edit
timers.json
Edit
timers.md
Edit
tls.html
Edit
tls.json
Edit
tls.md
Edit
tracing.html
Edit
tracing.json
Edit
tracing.md
Edit
tty.html
Edit
tty.json
Edit
tty.md
Edit
url.html
Edit
url.json
Edit
url.md
Edit
util.html
Edit
util.json
Edit
util.md
Edit
v8.html
Edit
v8.json
Edit
v8.md
Edit
vm.html
Edit
vm.json
Edit
vm.md
Edit
wasi.html
Edit
wasi.json
Edit
wasi.md
Edit
webcrypto.html
Edit
webcrypto.json
Edit
webcrypto.md
Edit
webstreams.html
Edit
webstreams.json
Edit
webstreams.md
Edit
worker_threads.html
Edit
worker_threads.json
Edit
worker_threads.md
Edit
zlib.html
Edit
zlib.json
Edit
zlib.md
Edit