@@ -43,10 +43,12 @@ import diagnostics_channel from 'node:diagnostics_channel';
4343// Get a reusable channel object
4444const channel = diagnostics_channel .channel (' my-channel' );
4545
46- // Subscribe to the channel
47- channel .subscribe ((message , name ) => {
46+ function onMessage (message , name ) {
4847 // Received data
49- });
48+ }
49+
50+ // Subscribe to the channel
51+ diagnostics_channel .subscribe (' my-channel' , onMessage);
5052
5153// Check if the channel has an active subscriber
5254if (channel .hasSubscribers ) {
@@ -55,6 +57,9 @@ if (channel.hasSubscribers) {
5557 some: ' data'
5658 });
5759}
60+
61+ // Unsubscribe from the channel
62+ diagnostics_channel .unsubscribe (' my-channel' , onMessage);
5863```
5964
6065``` cjs
@@ -63,10 +68,12 @@ const diagnostics_channel = require('node:diagnostics_channel');
6368// Get a reusable channel object
6469const channel = diagnostics_channel .channel (' my-channel' );
6570
66- // Subscribe to the channel
67- channel .subscribe ((message , name ) => {
71+ function onMessage (message , name ) {
6872 // Received data
69- });
73+ }
74+
75+ // Subscribe to the channel
76+ diagnostics_channel .subscribe (' my-channel' , onMessage);
7077
7178// Check if the channel has an active subscriber
7279if (channel .hasSubscribers ) {
@@ -75,6 +82,9 @@ if (channel.hasSubscribers) {
7582 some: ' data'
7683 });
7784}
85+
86+ // Unsubscribe from the channel
87+ diagnostics_channel .unsubscribe (' my-channel' , onMessage);
7888```
7989
8090#### ` diagnostics_channel.hasSubscribers(name) `
@@ -121,7 +131,7 @@ added:
121131* ` name ` {string|symbol} The channel name
122132* Returns: {Channel} The named channel object
123133
124- This is the primary entry-point for anyone wanting to interact with a named
134+ This is the primary entry-point for anyone wanting to publish to a named
125135channel. It produces a channel object which is optimized to reduce overhead at
126136publish time as much as possible.
127137
@@ -137,6 +147,76 @@ const diagnostics_channel = require('node:diagnostics_channel');
137147const channel = diagnostics_channel .channel (' my-channel' );
138148```
139149
150+ #### ` diagnostics_channel.subscribe(name, onMessage) `
151+
152+ <!-- YAML
153+ added:
154+ - REPLACEME
155+ -->
156+
157+ * ` name ` {string|symbol} The channel name
158+ * ` onMessage ` {Function} The handler to receive channel messages
159+ * ` message ` {any} The message data
160+ * ` name ` {string|symbol} The name of the channel
161+
162+ Register a message handler to subscribe to this channel. This message handler
163+ will be run synchronously whenever a message is published to the channel. Any
164+ errors thrown in the message handler will trigger an [ ` 'uncaughtException' ` ] [ ] .
165+
166+ ``` mjs
167+ import diagnostics_channel from ' diagnostics_channel' ;
168+
169+ diagnostics_channel .subscribe (' my-channel' , (message , name ) => {
170+ // Received data
171+ });
172+ ```
173+
174+ ``` cjs
175+ const diagnostics_channel = require (' diagnostics_channel' );
176+
177+ diagnostics_channel .subscribe (' my-channel' , (message , name ) => {
178+ // Received data
179+ });
180+ ```
181+
182+ #### ` diagnostics_channel.unsubscribe(name, onMessage) `
183+
184+ <!-- YAML
185+ added:
186+ - REPLACEME
187+ -->
188+
189+ * ` name ` {string|symbol} The channel name
190+ * ` onMessage ` {Function} The previous subscribed handler to remove
191+ * Returns: {boolean} ` true ` if the handler was found, ` false ` otherwise.
192+
193+ Remove a message handler previously registered to this channel with
194+ [ ` diagnostics_channel.subscribe(name, onMessage) ` ] [ ] .
195+
196+ ``` mjs
197+ import diagnostics_channel from ' diagnostics_channel' ;
198+
199+ function onMessage (message , name ) {
200+ // Received data
201+ }
202+
203+ diagnostics_channel .subscribe (' my-channel' , onMessage);
204+
205+ diagnostics_channel .unsubscribe (' my-channel' , onMessage);
206+ ```
207+
208+ ``` cjs
209+ const diagnostics_channel = require (' diagnostics_channel' );
210+
211+ function onMessage (message , name ) {
212+ // Received data
213+ }
214+
215+ diagnostics_channel .subscribe (' my-channel' , onMessage);
216+
217+ diagnostics_channel .unsubscribe (' my-channel' , onMessage);
218+ ```
219+
140220### Class: ` Channel `
141221
142222<!-- YAML
@@ -344,4 +424,5 @@ Emitted when server sends a response.
344424
345425[ `'uncaughtException'` ] : process.md#event-uncaughtexception
346426[ `channel.subscribe(onMessage)` ] : #channelsubscribeonmessage
427+ [ `diagnostics_channel.subscribe(name, onMessage)` ] : #diagnostics_channelunsubscribename_onmessage
347428[ `diagnostics_channel.channel(name)` ] : #diagnostics_channelchannelname
0 commit comments