{ "type": "module", "source": "doc/api/diagnostics_channel.md", "modules": [ { "textRaw": "Diagnostics Channel", "name": "diagnostics_channel", "introduced_in": "v15.1.0", "stability": 1, "stabilityText": "Experimental", "desc": "

Source Code: lib/diagnostics_channel.js

\n

The diagnostics_channel module provides an API to create named channels\nto report arbitrary message data for diagnostics purposes.

\n

It can be accessed using:

\n
const diagnostics_channel = require('diagnostics_channel');\n
\n

It is intended that a module writer wanting to report diagnostics messages\nwill create one or many top-level channels to report messages through.\nChannels may also be acquired at runtime but it is not encouraged\ndue to the additional overhead of doing so. Channels may be exported for\nconvenience, but as long as the name is known it can be acquired anywhere.

\n

If you intend for your module to produce diagnostics data for others to\nconsume it is recommended that you include documentation of what named\nchannels are used along with the shape of the message data. Channel names\nshould generally include the module name to avoid collisions with data from\nother modules.

", "modules": [ { "textRaw": "Public API", "name": "public_api", "modules": [ { "textRaw": "Overview", "name": "overview", "desc": "

Following is a simple overview of the public API.

\n
const diagnostics_channel = require('diagnostics_channel');\n\n// Get a reusable channel object\nconst channel = diagnostics_channel.channel('my-channel');\n\n// Subscribe to the channel\nchannel.subscribe((message, name) => {\n  // Received data\n});\n\n// Check if the channel has an active subscriber\nif (channel.hasSubscribers) {\n  // Publish data to the channel\n  channel.publish({\n    some: 'data'\n  });\n}\n
", "methods": [ { "textRaw": "`diagnostics_channel.hasSubscribers(name)`", "type": "method", "name": "hasSubscribers", "signatures": [ { "return": { "textRaw": "Returns: {boolean} If there are active subscribers", "name": "return", "type": "boolean", "desc": "If there are active subscribers" }, "params": [ { "textRaw": "`name` {string|symbol} The channel name", "name": "name", "type": "string|symbol", "desc": "The channel name" } ] } ], "desc": "

Check if there are active subscribers to the named channel. This is helpful if\nthe message you want to send might be expensive to prepare.

\n

This API is optional but helpful when trying to publish messages from very\nperformance-senstive code.

\n
const diagnostics_channel = require('diagnostics_channel');\n\nif (diagnostics_channel.hasSubscribers('my-channel')) {\n  // There are subscribers, prepare and publish message\n}\n
" }, { "textRaw": "`diagnostics_channel.channel(name)`", "type": "method", "name": "channel", "signatures": [ { "return": { "textRaw": "Returns: {Channel} The named channel object", "name": "return", "type": "Channel", "desc": "The named channel object" }, "params": [ { "textRaw": "`name` {string|symbol} The channel name", "name": "name", "type": "string|symbol", "desc": "The channel name" } ] } ], "desc": "

This is the primary entry-point for anyone wanting to interact with a named\nchannel. It produces a channel object which is optimized to reduce overhead at\npublish time as much as possible.

\n
const diagnostics_channel = require('diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n
" } ], "type": "module", "displayName": "Overview" } ], "classes": [ { "textRaw": "Class: `Channel`", "type": "class", "name": "Channel", "desc": "

The class Channel represents an individual named channel within the data\npipeline. It is use to track subscribers and to publish messages when there\nare subscribers present. It exists as a separate object to avoid channel\nlookups at publish time, enabling very fast publish speeds and allowing\nfor heavy use while incurring very minimal cost. Channels are created with\ndiagnostics_channel.channel(name), constructing a channel directly\nwith new Channel(name) is not supported.

", "properties": [ { "textRaw": "`hasSubscribers` Returns: {boolean} If there are active subscribers", "type": "boolean", "name": "return", "desc": "

Check if there are active subscribers to this channel. This is helpful if\nthe message you want to send might be expensive to prepare.

\n

This API is optional but helpful when trying to publish messages from very\nperformance-senstive code.

\n
const diagnostics_channel = require('diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nif (channel.hasSubscribers) {\n  // There are subscribers, prepare and publish message\n}\n
", "shortDesc": "If there are active subscribers" } ], "methods": [ { "textRaw": "`channel.publish(message)`", "type": "method", "name": "publish", "signatures": [ { "params": [ { "textRaw": "`message` {any} The message to send to the channel subscribers", "name": "message", "type": "any", "desc": "The message to send to the channel subscribers" } ] } ], "desc": "

Publish a message to any subscribers to the channel. This will trigger\nmessage handlers synchronously so they will execute within the same context.

\n
const diagnostics_channel = require('diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.publish({\n  some: 'message'\n});\n
" }, { "textRaw": "`channel.subscribe(onMessage)`", "type": "method", "name": "subscribe", "signatures": [ { "params": [ { "textRaw": "`onMessage` {Function} The handler to receive channel messages", "name": "onMessage", "type": "Function", "desc": "The handler to receive channel messages", "options": [ { "textRaw": "`message` {any} The message data", "name": "message", "type": "any", "desc": "The message data" }, { "textRaw": "`name` {string|symbol} The name of the channel", "name": "name", "type": "string|symbol", "desc": "The name of the channel" } ] } ] } ], "desc": "

Register a message handler to subscribe to this channel. This message handler\nwill be run synchronously whenever a message is published to the channel. Any\nerrors thrown in the message handler will trigger an 'uncaughtException'.

\n
const diagnostics_channel = require('diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nchannel.subscribe((message, name) => {\n  // Received data\n});\n
" }, { "textRaw": "`channel.unsubscribe(onMessage)`", "type": "method", "name": "unsubscribe", "signatures": [ { "params": [ { "textRaw": "`onMessage` {Function} The previous subscribed handler to remove", "name": "onMessage", "type": "Function", "desc": "The previous subscribed handler to remove" } ] } ], "desc": "

Remove a message handler previously registered to this channel with\nchannel.subscribe(onMessage).

\n
const diagnostics_channel = require('diagnostics_channel');\n\nconst channel = diagnostics_channel.channel('my-channel');\n\nfunction onMessage(message, name) {\n  // Received data\n}\n\nchannel.subscribe(onMessage);\n\nchannel.unsubscribe(onMessage);\n
" } ] } ], "type": "module", "displayName": "Public API" } ], "type": "module", "displayName": "Diagnostics Channel" } ] }