1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.18.2"> 7 <title>Diagnostics Channel | Node.js v18.18.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/diagnostics_channel.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:734px){.with-64-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-diagnostics_channel"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel active">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="diagnostics_channel" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#diagnostics-channel">Diagnostics Channel</a></span> 124<ul> 125<li><a href="#public-api">Public API</a> 126<ul> 127<li><a href="#overview">Overview</a> 128<ul> 129<li><a href="#diagnostics_channelhassubscribersname"><code>diagnostics_channel.hasSubscribers(name)</code></a></li> 130<li><a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a></li> 131<li><a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></li> 132<li><a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></li> 133</ul> 134</li> 135<li><a href="#class-channel">Class: <code>Channel</code></a> 136<ul> 137<li><a href="#channelhassubscribers"><code>channel.hasSubscribers</code></a></li> 138<li><a href="#channelpublishmessage"><code>channel.publish(message)</code></a></li> 139<li><span class="stability_0"><a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a></span></li> 140<li><span class="stability_0"><a href="#channelunsubscribeonmessage"><code>channel.unsubscribe(onMessage)</code></a></span></li> 141</ul> 142</li> 143<li><span class="stability_1"><a href="#built-in-channels">Built-in Channels</a></span> 144<ul> 145<li><a href="#http">HTTP</a></li> 146</ul> 147</li> 148</ul> 149</li> 150</ul> 151</li> 152</ul></div></div> 153 </li> 154 155 156 <li class="picker-header"> 157 <a href="#"> 158 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 159 Index 160 </a> 161 162 <div class="picker"><ul> 163<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 164<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 165 166 <li> 167 <a href="index.html">Index</a> 168 </li> 169 </ul> 170 171<hr class="line"> 172<ul> 173<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 174<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 175<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 176<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 177<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 178<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 179<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 180<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 181<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 182<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 183<li><a href="console.html" class="nav-console">Console</a></li> 184<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 185<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 186<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 187<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 188<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel active">Diagnostics Channel</a></li> 189<li><a href="dns.html" class="nav-dns">DNS</a></li> 190<li><a href="domain.html" class="nav-domain">Domain</a></li> 191<li><a href="errors.html" class="nav-errors">Errors</a></li> 192<li><a href="events.html" class="nav-events">Events</a></li> 193<li><a href="fs.html" class="nav-fs">File system</a></li> 194<li><a href="globals.html" class="nav-globals">Globals</a></li> 195<li><a href="http.html" class="nav-http">HTTP</a></li> 196<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 197<li><a href="https.html" class="nav-https">HTTPS</a></li> 198<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 199<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 200<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 201<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 202<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 203<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 204<li><a href="net.html" class="nav-net">Net</a></li> 205<li><a href="os.html" class="nav-os">OS</a></li> 206<li><a href="path.html" class="nav-path">Path</a></li> 207<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 208<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 209<li><a href="process.html" class="nav-process">Process</a></li> 210<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 211<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 212<li><a href="readline.html" class="nav-readline">Readline</a></li> 213<li><a href="repl.html" class="nav-repl">REPL</a></li> 214<li><a href="report.html" class="nav-report">Report</a></li> 215<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 216<li><a href="stream.html" class="nav-stream">Stream</a></li> 217<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 218<li><a href="test.html" class="nav-test">Test runner</a></li> 219<li><a href="timers.html" class="nav-timers">Timers</a></li> 220<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 221<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 222<li><a href="tty.html" class="nav-tty">TTY</a></li> 223<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 224<li><a href="url.html" class="nav-url">URL</a></li> 225<li><a href="util.html" class="nav-util">Utilities</a></li> 226<li><a href="v8.html" class="nav-v8">V8</a></li> 227<li><a href="vm.html" class="nav-vm">VM</a></li> 228<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 229<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 230<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 231<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 232<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 233</ul> 234<hr class="line"> 235<ul> 236<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 237</ul></div> 238 </li> 239 240 241 <li class="picker-header"> 242 <a href="#"> 243 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 244 Other versions 245 </a> 246 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/diagnostics_channel.html">20.x</a></li> 247<li><a href="https://nodejs.org/docs/latest-v19.x/api/diagnostics_channel.html">19.x</a></li> 248<li><a href="https://nodejs.org/docs/latest-v18.x/api/diagnostics_channel.html">18.x <b>LTS</b></a></li> 249<li><a href="https://nodejs.org/docs/latest-v17.x/api/diagnostics_channel.html">17.x</a></li> 250<li><a href="https://nodejs.org/docs/latest-v16.x/api/diagnostics_channel.html">16.x</a></li> 251<li><a href="https://nodejs.org/docs/latest-v15.x/api/diagnostics_channel.html">15.x</a></li></ol></div> 252 </li> 253 254 <li class="picker-header"> 255 <a href="#"> 256 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 257 Options 258 </a> 259 260 <div class="picker"> 261 <ul> 262 <li> 263 <a href="all.html">View on single page</a> 264 </li> 265 <li> 266 <a href="diagnostics_channel.json">View as JSON</a> 267 </li> 268 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/diagnostics_channel.md">Edit on GitHub</a></li> 269 </ul> 270 </div> 271 </li> 272 </ul> 273 </div> 274 <hr> 275 </header> 276 277 <details id="toc" open><summary>Table of contents</summary><ul> 278<li><span class="stability_2"><a href="#diagnostics-channel">Diagnostics Channel</a></span> 279<ul> 280<li><a href="#public-api">Public API</a> 281<ul> 282<li><a href="#overview">Overview</a> 283<ul> 284<li><a href="#diagnostics_channelhassubscribersname"><code>diagnostics_channel.hasSubscribers(name)</code></a></li> 285<li><a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a></li> 286<li><a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></li> 287<li><a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></li> 288</ul> 289</li> 290<li><a href="#class-channel">Class: <code>Channel</code></a> 291<ul> 292<li><a href="#channelhassubscribers"><code>channel.hasSubscribers</code></a></li> 293<li><a href="#channelpublishmessage"><code>channel.publish(message)</code></a></li> 294<li><span class="stability_0"><a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a></span></li> 295<li><span class="stability_0"><a href="#channelunsubscribeonmessage"><code>channel.unsubscribe(onMessage)</code></a></span></li> 296</ul> 297</li> 298<li><span class="stability_1"><a href="#built-in-channels">Built-in Channels</a></span> 299<ul> 300<li><a href="#http">HTTP</a></li> 301</ul> 302</li> 303</ul> 304</li> 305</ul> 306</li> 307</ul></details> 308 309 <div id="apicontent"> 310 <h2>Diagnostics Channel<span><a class="mark" href="#diagnostics-channel" id="diagnostics-channel">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel"></a></h2> 311<div class="api_metadata"> 312<details class="changelog"><summary>History</summary> 313<table> 314<tbody><tr><th>Version</th><th>Changes</th></tr> 315<tr><td>v18.13.0</td> 316<td><p>diagnostics_channel is now Stable.</p></td></tr> 317<tr><td>v15.1.0, v14.17.0</td> 318<td><p><span>Added in: v15.1.0, v14.17.0</span></p></td></tr> 319</tbody></table> 320</details> 321</div> 322 323<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 324<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/diagnostics_channel.js">lib/diagnostics_channel.js</a></p> 325<p>The <code>node:diagnostics_channel</code> module provides an API to create named channels 326to report arbitrary message data for diagnostics purposes.</p> 327<p>It can be accessed using:</p> 328 329<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>);</code><button class="copy-button">copy</button></pre> 330<p>It is intended that a module writer wanting to report diagnostics messages 331will create one or many top-level channels to report messages through. 332Channels may also be acquired at runtime but it is not encouraged 333due to the additional overhead of doing so. Channels may be exported for 334convenience, but as long as the name is known it can be acquired anywhere.</p> 335<p>If you intend for your module to produce diagnostics data for others to 336consume it is recommended that you include documentation of what named 337channels are used along with the shape of the message data. Channel names 338should generally include the module name to avoid collisions with data from 339other modules.</p> 340<section><h3>Public API<span><a class="mark" href="#public-api" id="public-api">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_public_api"></a></h3> 341<h4>Overview<span><a class="mark" href="#overview" id="overview">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_overview"></a></h4> 342<p>Following is a simple overview of the public API.</p> 343 344<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 345 346<span class="hljs-comment">// Get a reusable channel object</span> 347<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 348 349<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 350 <span class="hljs-comment">// Received data</span> 351} 352 353<span class="hljs-comment">// Subscribe to the channel</span> 354diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 355 356<span class="hljs-comment">// Check if the channel has an active subscriber</span> 357<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 358 <span class="hljs-comment">// Publish data to the channel</span> 359 channel.<span class="hljs-title function_">publish</span>({ 360 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>, 361 }); 362} 363 364<span class="hljs-comment">// Unsubscribe from the channel</span> 365diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 366 367<span class="hljs-comment">// Get a reusable channel object</span> 368<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 369 370<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 371 <span class="hljs-comment">// Received data</span> 372} 373 374<span class="hljs-comment">// Subscribe to the channel</span> 375diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 376 377<span class="hljs-comment">// Check if the channel has an active subscriber</span> 378<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 379 <span class="hljs-comment">// Publish data to the channel</span> 380 channel.<span class="hljs-title function_">publish</span>({ 381 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>, 382 }); 383} 384 385<span class="hljs-comment">// Unsubscribe from the channel</span> 386diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><button class="copy-button">copy</button></pre> 387<h5><code>diagnostics_channel.hasSubscribers(name)</code><span><a class="mark" href="#diagnostics_channelhassubscribersname" id="diagnostics_channelhassubscribersname">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_hassubscribers_name"></a></h5> 388<div class="api_metadata"> 389<span>Added in: v15.1.0, v14.17.0</span> 390</div> 391<ul> 392<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 393<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If there are active subscribers</li> 394</ul> 395<p>Check if there are active subscribers to the named channel. This is helpful if 396the message you want to send might be expensive to prepare.</p> 397<p>This API is optional but helpful when trying to publish messages from very 398performance-sensitive code.</p> 399 400<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 401 402<span class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscribers</span>(<span class="hljs-string">'my-channel'</span>)) { 403 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 404}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 405 406<span class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscribers</span>(<span class="hljs-string">'my-channel'</span>)) { 407 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 408}</code><button class="copy-button">copy</button></pre> 409<h5><code>diagnostics_channel.channel(name)</code><span><a class="mark" href="#diagnostics_channelchannelname" id="diagnostics_channelchannelname">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_channel_name"></a></h5> 410<div class="api_metadata"> 411<span>Added in: v15.1.0, v14.17.0</span> 412</div> 413<ul> 414<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 415<li>Returns: <a href="diagnostics_channel.html#class-channel" class="type"><Channel></a> The named channel object</li> 416</ul> 417<p>This is the primary entry-point for anyone wanting to publish to a named 418channel. It produces a channel object which is optimized to reduce overhead at 419publish time as much as possible.</p> 420 421<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 422 423<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 424 425<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>);</code><button class="copy-button">copy</button></pre> 426<h5><code>diagnostics_channel.subscribe(name, onMessage)</code><span><a class="mark" href="#diagnostics_channelsubscribename-onmessage" id="diagnostics_channelsubscribename-onmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_subscribe_name_onmessage"></a></h5> 427<div class="api_metadata"> 428<span>Added in: v18.7.0</span> 429</div> 430<ul> 431<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 432<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The handler to receive channel messages 433<ul> 434<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message data</li> 435<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The name of the channel</li> 436</ul> 437</li> 438</ul> 439<p>Register a message handler to subscribe to this channel. This message handler 440will be run synchronously whenever a message is published to the channel. Any 441errors thrown in the message handler will trigger an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a>.</p> 442 443<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 444 445diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, <span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 446 <span class="hljs-comment">// Received data</span> 447});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 448 449diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, <span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 450 <span class="hljs-comment">// Received data</span> 451});</code><button class="copy-button">copy</button></pre> 452<h5><code>diagnostics_channel.unsubscribe(name, onMessage)</code><span><a class="mark" href="#diagnostics_channelunsubscribename-onmessage" id="diagnostics_channelunsubscribename-onmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_diagnostics_channel_unsubscribe_name_onmessage"></a></h5> 453<div class="api_metadata"> 454<span>Added in: v18.7.0</span> 455</div> 456<ul> 457<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The channel name</li> 458<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The previous subscribed handler to remove</li> 459<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the handler was found, <code>false</code> otherwise.</li> 460</ul> 461<p>Remove a message handler previously registered to this channel with 462<a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a>.</p> 463 464<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 465 466<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 467 <span class="hljs-comment">// Received data</span> 468} 469 470diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 471 472diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 473 474<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 475 <span class="hljs-comment">// Received data</span> 476} 477 478diagnostics_channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage); 479 480diagnostics_channel.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-string">'my-channel'</span>, onMessage);</code><button class="copy-button">copy</button></pre> 481<h4>Class: <code>Channel</code><span><a class="mark" href="#class-channel" id="class-channel">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_class_channel"></a></h4> 482<div class="api_metadata"> 483<span>Added in: v15.1.0, v14.17.0</span> 484</div> 485<p>The class <code>Channel</code> represents an individual named channel within the data 486pipeline. It is used to track subscribers and to publish messages when there 487are subscribers present. It exists as a separate object to avoid channel 488lookups at publish time, enabling very fast publish speeds and allowing 489for heavy use while incurring very minimal cost. Channels are created with 490<a href="#diagnostics_channelchannelname"><code>diagnostics_channel.channel(name)</code></a>, constructing a channel directly 491with <code>new Channel(name)</code> is not supported.</p> 492<h5><code>channel.hasSubscribers</code><span><a class="mark" href="#channelhassubscribers" id="channelhassubscribers">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_hassubscribers"></a></h5> 493<div class="api_metadata"> 494<span>Added in: v15.1.0, v14.17.0</span> 495</div> 496<ul> 497<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If there are active subscribers</li> 498</ul> 499<p>Check if there are active subscribers to this channel. This is helpful if 500the message you want to send might be expensive to prepare.</p> 501<p>This API is optional but helpful when trying to publish messages from very 502performance-sensitive code.</p> 503 504<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 505 506<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 507 508<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 509 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 510}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 511 512<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 513 514<span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) { 515 <span class="hljs-comment">// There are subscribers, prepare and publish message</span> 516}</code><button class="copy-button">copy</button></pre> 517<h5><code>channel.publish(message)</code><span><a class="mark" href="#channelpublishmessage" id="channelpublishmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_publish_message"></a></h5> 518<div class="api_metadata"> 519<span>Added in: v15.1.0, v14.17.0</span> 520</div> 521<ul> 522<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message to send to the channel subscribers</li> 523</ul> 524<p>Publish a message to any subscribers to the channel. This will trigger 525message handlers synchronously so they will execute within the same context.</p> 526 527<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 528 529<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 530 531channel.<span class="hljs-title function_">publish</span>({ 532 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>, 533});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 534 535<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 536 537channel.<span class="hljs-title function_">publish</span>({ 538 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>, 539});</code><button class="copy-button">copy</button></pre> 540<h5><code>channel.subscribe(onMessage)</code><span><a class="mark" href="#channelsubscribeonmessage" id="channelsubscribeonmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_subscribe_onmessage"></a></h5> 541<div class="api_metadata"> 542<span>Added in: v15.1.0, v14.17.0</span><span>Deprecated since: v18.7.0</span> 543</div> 544<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#diagnostics_channelsubscribename-onmessage"><code>diagnostics_channel.subscribe(name, onMessage)</code></a></div><p></p> 545<ul> 546<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The handler to receive channel messages 547<ul> 548<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The message data</li> 549<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> The name of the channel</li> 550</ul> 551</li> 552</ul> 553<p>Register a message handler to subscribe to this channel. This message handler 554will be run synchronously whenever a message is published to the channel. Any 555errors thrown in the message handler will trigger an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a>.</p> 556 557<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 558 559<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 560 561channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 562 <span class="hljs-comment">// Received data</span> 563});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 564 565<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 566 567channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span class="hljs-params">message, name</span>) =></span> { 568 <span class="hljs-comment">// Received data</span> 569});</code><button class="copy-button">copy</button></pre> 570<h5><code>channel.unsubscribe(onMessage)</code><span><a class="mark" href="#channelunsubscribeonmessage" id="channelunsubscribeonmessage">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_channel_unsubscribe_onmessage"></a></h5> 571<div class="api_metadata"> 572<details class="changelog"><summary>History</summary> 573<table> 574<tbody><tr><th>Version</th><th>Changes</th></tr> 575<tr><td>v18.7.0</td> 576<td><p><span>Deprecated since: v18.7.0</span></p></td></tr> 577<tr><td>v17.1.0, v16.14.0, v14.19.0</td> 578<td><p>Added return value. Added to channels without subscribers.</p></td></tr> 579<tr><td>v15.1.0, v14.17.0</td> 580<td><p><span>Added in: v15.1.0, v14.17.0</span></p></td></tr> 581</tbody></table> 582</details> 583</div> 584<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#diagnostics_channelunsubscribename-onmessage"><code>diagnostics_channel.unsubscribe(name, onMessage)</code></a></div><p></p> 585<ul> 586<li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The previous subscribed handler to remove</li> 587<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the handler was found, <code>false</code> otherwise.</li> 588</ul> 589<p>Remove a message handler previously registered to this channel with 590<a href="#channelsubscribeonmessage"><code>channel.subscribe(onMessage)</code></a>.</p> 591 592<pre class="with-64-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> diagnostics_channel <span class="hljs-keyword">from</span> <span class="hljs-string">'node:diagnostics_channel'</span>; 593 594<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 595 596<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 597 <span class="hljs-comment">// Received data</span> 598} 599 600channel.<span class="hljs-title function_">subscribe</span>(onMessage); 601 602channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:diagnostics_channel'</span>); 603 604<span class="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">channel</span>(<span class="hljs-string">'my-channel'</span>); 605 606<span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<span class="hljs-params">message, name</span>) { 607 <span class="hljs-comment">// Received data</span> 608} 609 610channel.<span class="hljs-title function_">subscribe</span>(onMessage); 611 612channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code><button class="copy-button">copy</button></pre> 613<h4>Built-in Channels<span><a class="mark" href="#built-in-channels" id="built-in-channels">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_built_in_channels"></a></h4> 614<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 615<p>While the diagnostics_channel API is now considered stable, the built-in 616channels currently available are not. Each channel must be declared stable 617independently.</p> 618<h5>HTTP<span><a class="mark" href="#http" id="http">#</a></span><a aria-hidden="true" class="legacy" id="diagnostics_channel_http"></a></h5> 619<p><code>http.client.request.start</code></p> 620<ul> 621<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 622</ul> 623<p>Emitted when client starts a request.</p> 624<p><code>http.client.response.finish</code></p> 625<ul> 626<li><code>request</code> <a href="http.html#class-httpclientrequest" class="type"><http.ClientRequest></a></li> 627<li><code>response</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 628</ul> 629<p>Emitted when client receives a response.</p> 630<p><code>http.server.request.start</code></p> 631<ul> 632<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 633<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 634<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 635<li><code>server</code> <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 636</ul> 637<p>Emitted when server receives a request.</p> 638<p><code>http.server.response.finish</code></p> 639<ul> 640<li><code>request</code> <a href="http.html#class-httpincomingmessage" class="type"><http.IncomingMessage></a></li> 641<li><code>response</code> <a href="http.html#class-httpserverresponse" class="type"><http.ServerResponse></a></li> 642<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 643<li><code>server</code> <a href="http.html#class-httpserver" class="type"><http.Server></a></li> 644</ul> 645<p>Emitted when server sends a response.</p> 646<p><code>net.client.socket</code></p> 647<ul> 648<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 649</ul> 650<p>Emitted when a new TCP or pipe client socket is created.</p> 651<p><code>net.server.socket</code></p> 652<ul> 653<li><code>socket</code> <a href="net.html#class-netsocket" class="type"><net.Socket></a></li> 654</ul> 655<p>Emitted when a new TCP or pipe connection is received.</p> 656<p><code>udp.socket</code></p> 657<ul> 658<li><code>socket</code> <a href="dgram.html#class-dgramsocket" class="type"><dgram.Socket></a></li> 659</ul> 660<p>Emitted when a new UDP socket is created.</p></section> 661 <!-- API END --> 662 </div> 663 </div> 664 </div> 665</body> 666</html> 667