What is diagnostics_channel?
The diagnostics_channel package is a module that provides an API for creating named channels that allow publishers and subscribers to communicate via a publish/subscribe mechanism. It is useful for diagnostics and monitoring of Node.js applications.
What are diagnostics_channel's main functionalities?
Creating a channel
This code demonstrates how to create a new diagnostics channel named 'my-channel'.
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
Publishing to a channel
This code shows how to publish a message to the 'my-channel'. The message is an object with data that subscribers can listen for.
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.publish({ foo: 'bar' });
Subscribing to a channel
This code snippet demonstrates how to subscribe to the 'my-channel' and log the name of the channel and the message received.
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
console.log(name, message);
});
Checking if a channel has subscribers
This code checks if the 'my-channel' has any subscribers before publishing a message to it.
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
channel.publish({ foo: 'bar' });
}
Other packages similar to diagnostics_channel
eventemitter3
EventEmitter3 is a high-performance event emitter. It offers similar publish/subscribe functionality but is not specifically designed for diagnostics or monitoring purposes. It is more general-purpose.
debug
The debug package allows you to create debug instances that can be turned on and off based on environment variables. It is similar in that it is used for diagnostics, but it does not provide a channel-based publish/subscribe mechanism.
bunyan
Bunyan is a JSON logging library for Node.js services. It provides detailed logs and includes mechanisms for streams and serializers. While it is used for diagnostics, it is more focused on logging than the publish/subscribe pattern.
winston
Winston is another logging library for Node.js. Similar to Bunyan, it is used for diagnostics and monitoring but is focused on logging with multiple transports, rather than a channel-based communication system.
diagnostics_channel-polyfill
Exposes a polyfill for the Node.js module diagnostics_channel
.
As of now, the Node.js module diagnostics_channel
is only available in Node ^14.17.0 || >=15.1.0
.
This aim to allow using the same API on older versions of Node.
Install
npm i diagnostics_channel
Usage
Refer to the official documentation: https://nodejs.org/api/diagnostics_channel.html
Notes
- This module and the one included in Node core do NOT share the same channels, they live independently.
- Since
WeakReference
is not available, channels will NOT be garbage collected when no reference is held in user-land. An additional function is provided to do manual cleanup if needed: dc.deleteChannel()
. This should not be needed in a typical scenario. Only use this method if you know why you are doing it.
const dc = require('diagnostics_channel');
const a = dc.channel('test');
const b = dc.channel('test');
console.log(a === b);
dc.deleteChannel('test');
const c = dc.channel('test');
console.log(a === c);
- Since
ERR_INVALID_ARG_TYPE
is not available, a simplfied copy of this error is included. - Since
triggerUncaughtException()
is not available, if an exception is thrown in a subscriber, the polyfill will instead simply re-throw the error inside a process.nextTick()
, which has a similar behavior except when the process crashes because of that exception: the crash message will point to this polyfill instead of where the error was created (ie: in the subscriber).