1{ 2 "type": "module", 3 "source": "doc/api/dgram.md", 4 "modules": [ 5 { 6 "textRaw": "UDP/datagram sockets", 7 "name": "dgram", 8 "introduced_in": "v0.10.0", 9 "stability": 2, 10 "stabilityText": "Stable", 11 "desc": "<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v18.18.2/lib/dgram.js\">lib/dgram.js</a></p>\n<p>The <code>node:dgram</code> module provides an implementation of UDP datagram sockets.</p>\n<pre><code class=\"language-mjs\">import dgram from 'node:dgram';\n\nconst server = dgram.createSocket('udp4');\n\nserver.on('error', (err) => {\n console.error(`server error:\\n${err.stack}`);\n server.close();\n});\n\nserver.on('message', (msg, rinfo) => {\n console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);\n});\n\nserver.on('listening', () => {\n const address = server.address();\n console.log(`server listening ${address.address}:${address.port}`);\n});\n\nserver.bind(41234);\n// Prints: server listening 0.0.0.0:41234\n</code></pre>\n<pre><code class=\"language-cjs\">const dgram = require('node:dgram');\nconst server = dgram.createSocket('udp4');\n\nserver.on('error', (err) => {\n console.error(`server error:\\n${err.stack}`);\n server.close();\n});\n\nserver.on('message', (msg, rinfo) => {\n console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);\n});\n\nserver.on('listening', () => {\n const address = server.address();\n console.log(`server listening ${address.address}:${address.port}`);\n});\n\nserver.bind(41234);\n// Prints: server listening 0.0.0.0:41234\n</code></pre>", 12 "classes": [ 13 { 14 "textRaw": "Class: `dgram.Socket`", 15 "type": "class", 16 "name": "dgram.Socket", 17 "meta": { 18 "added": [ 19 "v0.1.99" 20 ], 21 "changes": [] 22 }, 23 "desc": "<ul>\n<li>Extends: <a href=\"events.html#class-eventemitter\" class=\"type\"><EventEmitter></a></li>\n</ul>\n<p>Encapsulates the datagram functionality.</p>\n<p>New instances of <code>dgram.Socket</code> are created using <a href=\"#dgramcreatesocketoptions-callback\"><code>dgram.createSocket()</code></a>.\nThe <code>new</code> keyword is not to be used to create <code>dgram.Socket</code> instances.</p>", 24 "events": [ 25 { 26 "textRaw": "Event: `'close'`", 27 "type": "event", 28 "name": "close", 29 "meta": { 30 "added": [ 31 "v0.1.99" 32 ], 33 "changes": [] 34 }, 35 "params": [], 36 "desc": "<p>The <code>'close'</code> event is emitted after a socket is closed with <a href=\"#socketclosecallback\"><code>close()</code></a>.\nOnce triggered, no new <code>'message'</code> events will be emitted on this socket.</p>" 37 }, 38 { 39 "textRaw": "Event: `'connect'`", 40 "type": "event", 41 "name": "connect", 42 "meta": { 43 "added": [ 44 "v12.0.0" 45 ], 46 "changes": [] 47 }, 48 "params": [], 49 "desc": "<p>The <code>'connect'</code> event is emitted after a socket is associated to a remote\naddress as a result of a successful <a href=\"#socketconnectport-address-callback\"><code>connect()</code></a> call.</p>" 50 }, 51 { 52 "textRaw": "Event: `'error'`", 53 "type": "event", 54 "name": "error", 55 "meta": { 56 "added": [ 57 "v0.1.99" 58 ], 59 "changes": [] 60 }, 61 "params": [ 62 { 63 "textRaw": "`exception` {Error}", 64 "name": "exception", 65 "type": "Error" 66 } 67 ], 68 "desc": "<p>The <code>'error'</code> event is emitted whenever any error occurs. The event handler\nfunction is passed a single <code>Error</code> object.</p>" 69 }, 70 { 71 "textRaw": "Event: `'listening'`", 72 "type": "event", 73 "name": "listening", 74 "meta": { 75 "added": [ 76 "v0.1.99" 77 ], 78 "changes": [] 79 }, 80 "params": [], 81 "desc": "<p>The <code>'listening'</code> event is emitted once the <code>dgram.Socket</code> is addressable and\ncan receive data. This happens either explicitly with <code>socket.bind()</code> or\nimplicitly the first time data is sent using <code>socket.send()</code>.\nUntil the <code>dgram.Socket</code> is listening, the underlying system resources do not\nexist and calls such as <code>socket.address()</code> and <code>socket.setTTL()</code> will fail.</p>" 82 }, 83 { 84 "textRaw": "Event: `'message'`", 85 "type": "event", 86 "name": "message", 87 "meta": { 88 "added": [ 89 "v0.1.99" 90 ], 91 "changes": [ 92 { 93 "version": "v18.4.0", 94 "pr-url": "https://github.com/nodejs/node/pull/43054", 95 "description": "The `family` property now returns a string instead of a number." 96 }, 97 { 98 "version": "v18.0.0", 99 "pr-url": "https://github.com/nodejs/node/pull/41431", 100 "description": "The `family` property now returns a number instead of a string." 101 } 102 ] 103 }, 104 "params": [], 105 "desc": "<p>The <code>'message'</code> event is emitted when a new datagram is available on a socket.\nThe event handler function is passed two arguments: <code>msg</code> and <code>rinfo</code>.</p>\n<ul>\n<li><code>msg</code> <a href=\"buffer.html#class-buffer\" class=\"type\"><Buffer></a> The message.</li>\n<li><code>rinfo</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> Remote address information.\n<ul>\n<li><code>address</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The sender address.</li>\n<li><code>family</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The address family (<code>'IPv4'</code> or <code>'IPv6'</code>).</li>\n<li><code>port</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The sender port.</li>\n<li><code>size</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The message size.</li>\n</ul>\n</li>\n</ul>\n<p>If the source address of the incoming packet is an IPv6 link-local\naddress, the interface name is added to the <code>address</code>. For\nexample, a packet received on the <code>en0</code> interface might have the\naddress field set to <code>'fe80::2618:1234:ab11:3b9c%en0'</code>, where <code>'%en0'</code>\nis the interface name as a zone ID suffix.</p>" 106 } 107 ], 108 "methods": [ 109 { 110 "textRaw": "`socket.addMembership(multicastAddress[, multicastInterface])`", 111 "type": "method", 112 "name": "addMembership", 113 "meta": { 114 "added": [ 115 "v0.6.9" 116 ], 117 "changes": [] 118 }, 119 "signatures": [ 120 { 121 "params": [ 122 { 123 "textRaw": "`multicastAddress` {string}", 124 "name": "multicastAddress", 125 "type": "string" 126 }, 127 { 128 "textRaw": "`multicastInterface` {string}", 129 "name": "multicastInterface", 130 "type": "string" 131 } 132 ] 133 } 134 ], 135 "desc": "<p>Tells the kernel to join a multicast group at the given <code>multicastAddress</code> and\n<code>multicastInterface</code> using the <code>IP_ADD_MEMBERSHIP</code> socket option. If the\n<code>multicastInterface</code> argument is not specified, the operating system will choose\none interface and will add membership to it. To add membership to every\navailable interface, call <code>addMembership</code> multiple times, once per interface.</p>\n<p>When called on an unbound socket, this method will implicitly bind to a random\nport, listening on all interfaces.</p>\n<p>When sharing a UDP socket across multiple <code>cluster</code> workers, the\n<code>socket.addMembership()</code> function must be called only once or an\n<code>EADDRINUSE</code> error will occur:</p>\n<pre><code class=\"language-mjs\">import cluster from 'node:cluster';\nimport dgram from 'node:dgram';\n\nif (cluster.isPrimary) {\n cluster.fork(); // Works ok.\n cluster.fork(); // Fails with EADDRINUSE.\n} else {\n const s = dgram.createSocket('udp4');\n s.bind(1234, () => {\n s.addMembership('224.0.0.114');\n });\n}\n</code></pre>\n<pre><code class=\"language-cjs\">const cluster = require('node:cluster');\nconst dgram = require('node:dgram');\n\nif (cluster.isPrimary) {\n cluster.fork(); // Works ok.\n cluster.fork(); // Fails with EADDRINUSE.\n} else {\n const s = dgram.createSocket('udp4');\n s.bind(1234, () => {\n s.addMembership('224.0.0.114');\n });\n}\n</code></pre>" 136 }, 137 { 138 "textRaw": "`socket.addSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`", 139 "type": "method", 140 "name": "addSourceSpecificMembership", 141 "meta": { 142 "added": [ 143 "v13.1.0", 144 "v12.16.0" 145 ], 146 "changes": [] 147 }, 148 "signatures": [ 149 { 150 "params": [ 151 { 152 "textRaw": "`sourceAddress` {string}", 153 "name": "sourceAddress", 154 "type": "string" 155 }, 156 { 157 "textRaw": "`groupAddress` {string}", 158 "name": "groupAddress", 159 "type": "string" 160 }, 161 { 162 "textRaw": "`multicastInterface` {string}", 163 "name": "multicastInterface", 164 "type": "string" 165 } 166 ] 167 } 168 ], 169 "desc": "<p>Tells the kernel to join a source-specific multicast channel at the given\n<code>sourceAddress</code> and <code>groupAddress</code>, using the <code>multicastInterface</code> with the\n<code>IP_ADD_SOURCE_MEMBERSHIP</code> socket option. If the <code>multicastInterface</code> argument\nis not specified, the operating system will choose one interface and will add\nmembership to it. To add membership to every available interface, call\n<code>socket.addSourceSpecificMembership()</code> multiple times, once per interface.</p>\n<p>When called on an unbound socket, this method will implicitly bind to a random\nport, listening on all interfaces.</p>" 170 }, 171 { 172 "textRaw": "`socket.address()`", 173 "type": "method", 174 "name": "address", 175 "meta": { 176 "added": [ 177 "v0.1.99" 178 ], 179 "changes": [] 180 }, 181 "signatures": [ 182 { 183 "return": { 184 "textRaw": "Returns: {Object}", 185 "name": "return", 186 "type": "Object" 187 }, 188 "params": [] 189 } 190 ], 191 "desc": "<p>Returns an object containing the address information for a socket.\nFor UDP sockets, this object will contain <code>address</code>, <code>family</code>, and <code>port</code>\nproperties.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>" 192 }, 193 { 194 "textRaw": "`socket.bind([port][, address][, callback])`", 195 "type": "method", 196 "name": "bind", 197 "meta": { 198 "added": [ 199 "v0.1.99" 200 ], 201 "changes": [ 202 { 203 "version": "v0.9.1", 204 "commit": "332fea5ac1816e498030109c4211bca24a7fa667", 205 "description": "The method was changed to an asynchronous execution model. Legacy code would need to be changed to pass a callback function to the method call." 206 } 207 ] 208 }, 209 "signatures": [ 210 { 211 "params": [ 212 { 213 "textRaw": "`port` {integer}", 214 "name": "port", 215 "type": "integer" 216 }, 217 { 218 "textRaw": "`address` {string}", 219 "name": "address", 220 "type": "string" 221 }, 222 { 223 "textRaw": "`callback` {Function} with no parameters. Called when binding is complete.", 224 "name": "callback", 225 "type": "Function", 226 "desc": "with no parameters. Called when binding is complete." 227 } 228 ] 229 } 230 ], 231 "desc": "<p>For UDP sockets, causes the <code>dgram.Socket</code> to listen for datagram\nmessages on a named <code>port</code> and optional <code>address</code>. If <code>port</code> is not\nspecified or is <code>0</code>, the operating system will attempt to bind to a\nrandom port. If <code>address</code> is not specified, the operating system will\nattempt to listen on all addresses. Once binding is complete, a\n<code>'listening'</code> event is emitted and the optional <code>callback</code> function is\ncalled.</p>\n<p>Specifying both a <code>'listening'</code> event listener and passing a\n<code>callback</code> to the <code>socket.bind()</code> method is not harmful but not very\nuseful.</p>\n<p>A bound datagram socket keeps the Node.js process running to receive\ndatagram messages.</p>\n<p>If binding fails, an <code>'error'</code> event is generated. In rare case (e.g.\nattempting to bind with a closed socket), an <a href=\"errors.html#class-error\"><code>Error</code></a> may be thrown.</p>\n<p>Example of a UDP server listening on port 41234:</p>\n<pre><code class=\"language-mjs\">import dgram from 'node:dgram';\n\nconst server = dgram.createSocket('udp4');\n\nserver.on('error', (err) => {\n console.error(`server error:\\n${err.stack}`);\n server.close();\n});\n\nserver.on('message', (msg, rinfo) => {\n console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);\n});\n\nserver.on('listening', () => {\n const address = server.address();\n console.log(`server listening ${address.address}:${address.port}`);\n});\n\nserver.bind(41234);\n// Prints: server listening 0.0.0.0:41234\n</code></pre>\n<pre><code class=\"language-cjs\">const dgram = require('node:dgram');\nconst server = dgram.createSocket('udp4');\n\nserver.on('error', (err) => {\n console.error(`server error:\\n${err.stack}`);\n server.close();\n});\n\nserver.on('message', (msg, rinfo) => {\n console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);\n});\n\nserver.on('listening', () => {\n const address = server.address();\n console.log(`server listening ${address.address}:${address.port}`);\n});\n\nserver.bind(41234);\n// Prints: server listening 0.0.0.0:41234\n</code></pre>" 232 }, 233 { 234 "textRaw": "`socket.bind(options[, callback])`", 235 "type": "method", 236 "name": "bind", 237 "meta": { 238 "added": [ 239 "v0.11.14" 240 ], 241 "changes": [] 242 }, 243 "signatures": [ 244 { 245 "params": [ 246 { 247 "textRaw": "`options` {Object} Required. Supports the following properties:", 248 "name": "options", 249 "type": "Object", 250 "desc": "Required. Supports the following properties:", 251 "options": [ 252 { 253 "textRaw": "`port` {integer}", 254 "name": "port", 255 "type": "integer" 256 }, 257 { 258 "textRaw": "`address` {string}", 259 "name": "address", 260 "type": "string" 261 }, 262 { 263 "textRaw": "`exclusive` {boolean}", 264 "name": "exclusive", 265 "type": "boolean" 266 }, 267 { 268 "textRaw": "`fd` {integer}", 269 "name": "fd", 270 "type": "integer" 271 } 272 ] 273 }, 274 { 275 "textRaw": "`callback` {Function}", 276 "name": "callback", 277 "type": "Function" 278 } 279 ] 280 } 281 ], 282 "desc": "<p>For UDP sockets, causes the <code>dgram.Socket</code> to listen for datagram\nmessages on a named <code>port</code> and optional <code>address</code> that are passed as\nproperties of an <code>options</code> object passed as the first argument. If\n<code>port</code> is not specified or is <code>0</code>, the operating system will attempt\nto bind to a random port. If <code>address</code> is not specified, the operating\nsystem will attempt to listen on all addresses. Once binding is\ncomplete, a <code>'listening'</code> event is emitted and the optional <code>callback</code>\nfunction is called.</p>\n<p>The <code>options</code> object may contain a <code>fd</code> property. When a <code>fd</code> greater\nthan <code>0</code> is set, it will wrap around an existing socket with the given\nfile descriptor. In this case, the properties of <code>port</code> and <code>address</code>\nwill be ignored.</p>\n<p>Specifying both a <code>'listening'</code> event listener and passing a\n<code>callback</code> to the <code>socket.bind()</code> method is not harmful but not very\nuseful.</p>\n<p>The <code>options</code> object may contain an additional <code>exclusive</code> property that is\nused when using <code>dgram.Socket</code> objects with the <a href=\"cluster.html\"><code>cluster</code></a> module. When\n<code>exclusive</code> is set to <code>false</code> (the default), cluster workers will use the same\nunderlying socket handle allowing connection handling duties to be shared.\nWhen <code>exclusive</code> is <code>true</code>, however, the handle is not shared and attempted\nport sharing results in an error.</p>\n<p>A bound datagram socket keeps the Node.js process running to receive\ndatagram messages.</p>\n<p>If binding fails, an <code>'error'</code> event is generated. In rare case (e.g.\nattempting to bind with a closed socket), an <a href=\"errors.html#class-error\"><code>Error</code></a> may be thrown.</p>\n<p>An example socket listening on an exclusive port is shown below.</p>\n<pre><code class=\"language-js\">socket.bind({\n address: 'localhost',\n port: 8000,\n exclusive: true,\n});\n</code></pre>" 283 }, 284 { 285 "textRaw": "`socket.close([callback])`", 286 "type": "method", 287 "name": "close", 288 "meta": { 289 "added": [ 290 "v0.1.99" 291 ], 292 "changes": [] 293 }, 294 "signatures": [ 295 { 296 "params": [ 297 { 298 "textRaw": "`callback` {Function} Called when the socket has been closed.", 299 "name": "callback", 300 "type": "Function", 301 "desc": "Called when the socket has been closed." 302 } 303 ] 304 } 305 ], 306 "desc": "<p>Close the underlying socket and stop listening for data on it. If a callback is\nprovided, it is added as a listener for the <a href=\"#event-close\"><code>'close'</code></a> event.</p>" 307 }, 308 { 309 "textRaw": "`socket[Symbol.asyncDispose]()`", 310 "type": "method", 311 "name": "[Symbol.asyncDispose]", 312 "meta": { 313 "added": [ 314 "v18.18.0" 315 ], 316 "changes": [] 317 }, 318 "stability": 1, 319 "stabilityText": "Experimental", 320 "signatures": [ 321 { 322 "params": [] 323 } 324 ], 325 "desc": "<p>Calls <a href=\"#socketclosecallback\"><code>socket.close()</code></a> and returns a promise that fulfills when the\nsocket has closed.</p>" 326 }, 327 { 328 "textRaw": "`socket.connect(port[, address][, callback])`", 329 "type": "method", 330 "name": "connect", 331 "meta": { 332 "added": [ 333 "v12.0.0" 334 ], 335 "changes": [] 336 }, 337 "signatures": [ 338 { 339 "params": [ 340 { 341 "textRaw": "`port` {integer}", 342 "name": "port", 343 "type": "integer" 344 }, 345 { 346 "textRaw": "`address` {string}", 347 "name": "address", 348 "type": "string" 349 }, 350 { 351 "textRaw": "`callback` {Function} Called when the connection is completed or on error.", 352 "name": "callback", 353 "type": "Function", 354 "desc": "Called when the connection is completed or on error." 355 } 356 ] 357 } 358 ], 359 "desc": "<p>Associates the <code>dgram.Socket</code> to a remote address and port. Every\nmessage sent by this handle is automatically sent to that destination. Also,\nthe socket will only receive messages from that remote peer.\nTrying to call <code>connect()</code> on an already connected socket will result\nin an <a href=\"errors.html#err_socket_dgram_is_connected\"><code>ERR_SOCKET_DGRAM_IS_CONNECTED</code></a> exception. If <code>address</code> is not\nprovided, <code>'127.0.0.1'</code> (for <code>udp4</code> sockets) or <code>'::1'</code> (for <code>udp6</code> sockets)\nwill be used by default. Once the connection is complete, a <code>'connect'</code> event\nis emitted and the optional <code>callback</code> function is called. In case of failure,\nthe <code>callback</code> is called or, failing this, an <code>'error'</code> event is emitted.</p>" 360 }, 361 { 362 "textRaw": "`socket.disconnect()`", 363 "type": "method", 364 "name": "disconnect", 365 "meta": { 366 "added": [ 367 "v12.0.0" 368 ], 369 "changes": [] 370 }, 371 "signatures": [ 372 { 373 "params": [] 374 } 375 ], 376 "desc": "<p>A synchronous function that disassociates a connected <code>dgram.Socket</code> from\nits remote address. Trying to call <code>disconnect()</code> on an unbound or already\ndisconnected socket will result in an <a href=\"errors.html#err_socket_dgram_not_connected\"><code>ERR_SOCKET_DGRAM_NOT_CONNECTED</code></a>\nexception.</p>" 377 }, 378 { 379 "textRaw": "`socket.dropMembership(multicastAddress[, multicastInterface])`", 380 "type": "method", 381 "name": "dropMembership", 382 "meta": { 383 "added": [ 384 "v0.6.9" 385 ], 386 "changes": [] 387 }, 388 "signatures": [ 389 { 390 "params": [ 391 { 392 "textRaw": "`multicastAddress` {string}", 393 "name": "multicastAddress", 394 "type": "string" 395 }, 396 { 397 "textRaw": "`multicastInterface` {string}", 398 "name": "multicastInterface", 399 "type": "string" 400 } 401 ] 402 } 403 ], 404 "desc": "<p>Instructs the kernel to leave a multicast group at <code>multicastAddress</code> using the\n<code>IP_DROP_MEMBERSHIP</code> socket option. This method is automatically called by the\nkernel when the socket is closed or the process terminates, so most apps will\nnever have reason to call this.</p>\n<p>If <code>multicastInterface</code> is not specified, the operating system will attempt to\ndrop membership on all valid interfaces.</p>" 405 }, 406 { 407 "textRaw": "`socket.dropSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`", 408 "type": "method", 409 "name": "dropSourceSpecificMembership", 410 "meta": { 411 "added": [ 412 "v13.1.0", 413 "v12.16.0" 414 ], 415 "changes": [] 416 }, 417 "signatures": [ 418 { 419 "params": [ 420 { 421 "textRaw": "`sourceAddress` {string}", 422 "name": "sourceAddress", 423 "type": "string" 424 }, 425 { 426 "textRaw": "`groupAddress` {string}", 427 "name": "groupAddress", 428 "type": "string" 429 }, 430 { 431 "textRaw": "`multicastInterface` {string}", 432 "name": "multicastInterface", 433 "type": "string" 434 } 435 ] 436 } 437 ], 438 "desc": "<p>Instructs the kernel to leave a source-specific multicast channel at the given\n<code>sourceAddress</code> and <code>groupAddress</code> using the <code>IP_DROP_SOURCE_MEMBERSHIP</code>\nsocket option. This method is automatically called by the kernel when the\nsocket is closed or the process terminates, so most apps will never have\nreason to call this.</p>\n<p>If <code>multicastInterface</code> is not specified, the operating system will attempt to\ndrop membership on all valid interfaces.</p>" 439 }, 440 { 441 "textRaw": "`socket.getRecvBufferSize()`", 442 "type": "method", 443 "name": "getRecvBufferSize", 444 "meta": { 445 "added": [ 446 "v8.7.0" 447 ], 448 "changes": [] 449 }, 450 "signatures": [ 451 { 452 "return": { 453 "textRaw": "Returns: {number} the `SO_RCVBUF` socket receive buffer size in bytes.", 454 "name": "return", 455 "type": "number", 456 "desc": "the `SO_RCVBUF` socket receive buffer size in bytes." 457 }, 458 "params": [] 459 } 460 ], 461 "desc": "<p>This method throws <a href=\"errors.html#err_socket_buffer_size\"><code>ERR_SOCKET_BUFFER_SIZE</code></a> if called on an unbound socket.</p>" 462 }, 463 { 464 "textRaw": "`socket.getSendBufferSize()`", 465 "type": "method", 466 "name": "getSendBufferSize", 467 "meta": { 468 "added": [ 469 "v8.7.0" 470 ], 471 "changes": [] 472 }, 473 "signatures": [ 474 { 475 "return": { 476 "textRaw": "Returns: {number} the `SO_SNDBUF` socket send buffer size in bytes.", 477 "name": "return", 478 "type": "number", 479 "desc": "the `SO_SNDBUF` socket send buffer size in bytes." 480 }, 481 "params": [] 482 } 483 ], 484 "desc": "<p>This method throws <a href=\"errors.html#err_socket_buffer_size\"><code>ERR_SOCKET_BUFFER_SIZE</code></a> if called on an unbound socket.</p>" 485 }, 486 { 487 "textRaw": "`socket.getSendQueueSize()`", 488 "type": "method", 489 "name": "getSendQueueSize", 490 "meta": { 491 "added": [ 492 "v18.8.0" 493 ], 494 "changes": [] 495 }, 496 "signatures": [ 497 { 498 "return": { 499 "textRaw": "Returns: {number} Number of bytes queued for sending.", 500 "name": "return", 501 "type": "number", 502 "desc": "Number of bytes queued for sending." 503 }, 504 "params": [] 505 } 506 ] 507 }, 508 { 509 "textRaw": "`socket.getSendQueueCount()`", 510 "type": "method", 511 "name": "getSendQueueCount", 512 "meta": { 513 "added": [ 514 "v18.8.0" 515 ], 516 "changes": [] 517 }, 518 "signatures": [ 519 { 520 "return": { 521 "textRaw": "Returns: {number} Number of send requests currently in the queue awaiting to be processed.", 522 "name": "return", 523 "type": "number", 524 "desc": "Number of send requests currently in the queue awaiting to be processed." 525 }, 526 "params": [] 527 } 528 ] 529 }, 530 { 531 "textRaw": "`socket.ref()`", 532 "type": "method", 533 "name": "ref", 534 "meta": { 535 "added": [ 536 "v0.9.1" 537 ], 538 "changes": [] 539 }, 540 "signatures": [ 541 { 542 "return": { 543 "textRaw": "Returns: {dgram.Socket}", 544 "name": "return", 545 "type": "dgram.Socket" 546 }, 547 "params": [] 548 } 549 ], 550 "desc": "<p>By default, binding a socket will cause it to block the Node.js process from\nexiting as long as the socket is open. The <code>socket.unref()</code> method can be used\nto exclude the socket from the reference counting that keeps the Node.js\nprocess active. The <code>socket.ref()</code> method adds the socket back to the reference\ncounting and restores the default behavior.</p>\n<p>Calling <code>socket.ref()</code> multiples times will have no additional effect.</p>\n<p>The <code>socket.ref()</code> method returns a reference to the socket so calls can be\nchained.</p>" 551 }, 552 { 553 "textRaw": "`socket.remoteAddress()`", 554 "type": "method", 555 "name": "remoteAddress", 556 "meta": { 557 "added": [ 558 "v12.0.0" 559 ], 560 "changes": [] 561 }, 562 "signatures": [ 563 { 564 "return": { 565 "textRaw": "Returns: {Object}", 566 "name": "return", 567 "type": "Object" 568 }, 569 "params": [] 570 } 571 ], 572 "desc": "<p>Returns an object containing the <code>address</code>, <code>family</code>, and <code>port</code> of the remote\nendpoint. This method throws an <a href=\"errors.html#err_socket_dgram_not_connected\"><code>ERR_SOCKET_DGRAM_NOT_CONNECTED</code></a> exception\nif the socket is not connected.</p>" 573 }, 574 { 575 "textRaw": "`socket.send(msg[, offset, length][, port][, address][, callback])`", 576 "type": "method", 577 "name": "send", 578 "meta": { 579 "added": [ 580 "v0.1.99" 581 ], 582 "changes": [ 583 { 584 "version": "v17.0.0", 585 "pr-url": "https://github.com/nodejs/node/pull/39190", 586 "description": "The `address` parameter now only accepts a `string`, `null` or `undefined`." 587 }, 588 { 589 "version": [ 590 "v14.5.0", 591 "v12.19.0" 592 ], 593 "pr-url": "https://github.com/nodejs/node/pull/22413", 594 "description": "The `msg` parameter can now be any `TypedArray` or `DataView`." 595 }, 596 { 597 "version": "v12.0.0", 598 "pr-url": "https://github.com/nodejs/node/pull/26871", 599 "description": "Added support for sending data on connected sockets." 600 }, 601 { 602 "version": "v8.0.0", 603 "pr-url": "https://github.com/nodejs/node/pull/11985", 604 "description": "The `msg` parameter can be an `Uint8Array` now." 605 }, 606 { 607 "version": "v8.0.0", 608 "pr-url": "https://github.com/nodejs/node/pull/10473", 609 "description": "The `address` parameter is always optional now." 610 }, 611 { 612 "version": "v6.0.0", 613 "pr-url": "https://github.com/nodejs/node/pull/5929", 614 "description": "On success, `callback` will now be called with an `error` argument of `null` rather than `0`." 615 }, 616 { 617 "version": "v5.7.0", 618 "pr-url": "https://github.com/nodejs/node/pull/4374", 619 "description": "The `msg` parameter can be an array now. Also, the `offset` and `length` parameters are optional now." 620 } 621 ] 622 }, 623 "signatures": [ 624 { 625 "params": [ 626 { 627 "textRaw": "`msg` {Buffer|TypedArray|DataView|string|Array} Message to be sent.", 628 "name": "msg", 629 "type": "Buffer|TypedArray|DataView|string|Array", 630 "desc": "Message to be sent." 631 }, 632 { 633 "textRaw": "`offset` {integer} Offset in the buffer where the message starts.", 634 "name": "offset", 635 "type": "integer", 636 "desc": "Offset in the buffer where the message starts." 637 }, 638 { 639 "textRaw": "`length` {integer} Number of bytes in the message.", 640 "name": "length", 641 "type": "integer", 642 "desc": "Number of bytes in the message." 643 }, 644 { 645 "textRaw": "`port` {integer} Destination port.", 646 "name": "port", 647 "type": "integer", 648 "desc": "Destination port." 649 }, 650 { 651 "textRaw": "`address` {string} Destination host name or IP address.", 652 "name": "address", 653 "type": "string", 654 "desc": "Destination host name or IP address." 655 }, 656 { 657 "textRaw": "`callback` {Function} Called when the message has been sent.", 658 "name": "callback", 659 "type": "Function", 660 "desc": "Called when the message has been sent." 661 } 662 ] 663 } 664 ], 665 "desc": "<p>Broadcasts a datagram on the socket.\nFor connectionless sockets, the destination <code>port</code> and <code>address</code> must be\nspecified. Connected sockets, on the other hand, will use their associated\nremote endpoint, so the <code>port</code> and <code>address</code> arguments must not be set.</p>\n<p>The <code>msg</code> argument contains the message to be sent.\nDepending on its type, different behavior can apply. If <code>msg</code> is a <code>Buffer</code>,\nany <code>TypedArray</code> or a <code>DataView</code>,\nthe <code>offset</code> and <code>length</code> specify the offset within the <code>Buffer</code> where the\nmessage begins and the number of bytes in the message, respectively.\nIf <code>msg</code> is a <code>String</code>, then it is automatically converted to a <code>Buffer</code>\nwith <code>'utf8'</code> encoding. With messages that\ncontain multi-byte characters, <code>offset</code> and <code>length</code> will be calculated with\nrespect to <a href=\"buffer.html#static-method-bufferbytelengthstring-encoding\">byte length</a> and not the character position.\nIf <code>msg</code> is an array, <code>offset</code> and <code>length</code> must not be specified.</p>\n<p>The <code>address</code> argument is a string. If the value of <code>address</code> is a host name,\nDNS will be used to resolve the address of the host. If <code>address</code> is not\nprovided or otherwise nullish, <code>'127.0.0.1'</code> (for <code>udp4</code> sockets) or <code>'::1'</code>\n(for <code>udp6</code> sockets) will be used by default.</p>\n<p>If the socket has not been previously bound with a call to <code>bind</code>, the socket\nis assigned a random port number and is bound to the \"all interfaces\" address\n(<code>'0.0.0.0'</code> for <code>udp4</code> sockets, <code>'::0'</code> for <code>udp6</code> sockets.)</p>\n<p>An optional <code>callback</code> function may be specified to as a way of reporting\nDNS errors or for determining when it is safe to reuse the <code>buf</code> object.\nDNS lookups delay the time to send for at least one tick of the\nNode.js event loop.</p>\n<p>The only way to know for sure that the datagram has been sent is by using a\n<code>callback</code>. If an error occurs and a <code>callback</code> is given, the error will be\npassed as the first argument to the <code>callback</code>. If a <code>callback</code> is not given,\nthe error is emitted as an <code>'error'</code> event on the <code>socket</code> object.</p>\n<p>Offset and length are optional but both <em>must</em> be set if either are used.\nThey are supported only when the first argument is a <code>Buffer</code>, a <code>TypedArray</code>,\nor a <code>DataView</code>.</p>\n<p>This method throws <a href=\"errors.html#err_socket_bad_port\"><code>ERR_SOCKET_BAD_PORT</code></a> if called on an unbound socket.</p>\n<p>Example of sending a UDP packet to a port on <code>localhost</code>;</p>\n<pre><code class=\"language-mjs\">import dgram from 'node:dgram';\nimport { Buffer } from 'node:buffer';\n\nconst message = Buffer.from('Some bytes');\nconst client = dgram.createSocket('udp4');\nclient.send(message, 41234, 'localhost', (err) => {\n client.close();\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const dgram = require('node:dgram');\nconst { Buffer } = require('node:buffer');\n\nconst message = Buffer.from('Some bytes');\nconst client = dgram.createSocket('udp4');\nclient.send(message, 41234, 'localhost', (err) => {\n client.close();\n});\n</code></pre>\n<p>Example of sending a UDP packet composed of multiple buffers to a port on\n<code>127.0.0.1</code>;</p>\n<pre><code class=\"language-mjs\">import dgram from 'node:dgram';\nimport { Buffer } from 'node:buffer';\n\nconst buf1 = Buffer.from('Some ');\nconst buf2 = Buffer.from('bytes');\nconst client = dgram.createSocket('udp4');\nclient.send([buf1, buf2], 41234, (err) => {\n client.close();\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const dgram = require('node:dgram');\nconst { Buffer } = require('node:buffer');\n\nconst buf1 = Buffer.from('Some ');\nconst buf2 = Buffer.from('bytes');\nconst client = dgram.createSocket('udp4');\nclient.send([buf1, buf2], 41234, (err) => {\n client.close();\n});\n</code></pre>\n<p>Sending multiple buffers might be faster or slower depending on the\napplication and operating system. Run benchmarks to\ndetermine the optimal strategy on a case-by-case basis. Generally speaking,\nhowever, sending multiple buffers is faster.</p>\n<p>Example of sending a UDP packet using a socket connected to a port on\n<code>localhost</code>:</p>\n<pre><code class=\"language-mjs\">import dgram from 'node:dgram';\nimport { Buffer } from 'node:buffer';\n\nconst message = Buffer.from('Some bytes');\nconst client = dgram.createSocket('udp4');\nclient.connect(41234, 'localhost', (err) => {\n client.send(message, (err) => {\n client.close();\n });\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const dgram = require('node:dgram');\nconst { Buffer } = require('node:buffer');\n\nconst message = Buffer.from('Some bytes');\nconst client = dgram.createSocket('udp4');\nclient.connect(41234, 'localhost', (err) => {\n client.send(message, (err) => {\n client.close();\n });\n});\n</code></pre>", 666 "modules": [ 667 { 668 "textRaw": "Note about UDP datagram size", 669 "name": "note_about_udp_datagram_size", 670 "desc": "<p>The maximum size of an IPv4/v6 datagram depends on the <code>MTU</code>\n(Maximum Transmission Unit) and on the <code>Payload Length</code> field size.</p>\n<ul>\n<li>\n<p>The <code>Payload Length</code> field is 16 bits wide, which means that a normal\npayload cannot exceed 64K octets including the internet header and data\n(65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);\nthis is generally true for loopback interfaces, but such long datagram\nmessages are impractical for most hosts and networks.</p>\n</li>\n<li>\n<p>The <code>MTU</code> is the largest size a given link layer technology can support for\ndatagram messages. For any link, IPv4 mandates a minimum <code>MTU</code> of 68\noctets, while the recommended <code>MTU</code> for IPv4 is 576 (typically recommended\nas the <code>MTU</code> for dial-up type applications), whether they arrive whole or in\nfragments.</p>\n<p>For IPv6, the minimum <code>MTU</code> is 1280 octets. However, the mandatory minimum\nfragment reassembly buffer size is 1500 octets. The value of 68 octets is\nvery small, since most current link layer technologies, like Ethernet, have a\nminimum <code>MTU</code> of 1500.</p>\n</li>\n</ul>\n<p>It is impossible to know in advance the MTU of each link through which\na packet might travel. Sending a datagram greater than the receiver <code>MTU</code> will\nnot work because the packet will get silently dropped without informing the\nsource that the data did not reach its intended recipient.</p>", 671 "type": "module", 672 "displayName": "Note about UDP datagram size" 673 } 674 ] 675 }, 676 { 677 "textRaw": "`socket.setBroadcast(flag)`", 678 "type": "method", 679 "name": "setBroadcast", 680 "meta": { 681 "added": [ 682 "v0.6.9" 683 ], 684 "changes": [] 685 }, 686 "signatures": [ 687 { 688 "params": [ 689 { 690 "textRaw": "`flag` {boolean}", 691 "name": "flag", 692 "type": "boolean" 693 } 694 ] 695 } 696 ], 697 "desc": "<p>Sets or clears the <code>SO_BROADCAST</code> socket option. When set to <code>true</code>, UDP\npackets may be sent to a local interface's broadcast address.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>" 698 }, 699 { 700 "textRaw": "`socket.setMulticastInterface(multicastInterface)`", 701 "type": "method", 702 "name": "setMulticastInterface", 703 "meta": { 704 "added": [ 705 "v8.6.0" 706 ], 707 "changes": [] 708 }, 709 "signatures": [ 710 { 711 "params": [ 712 { 713 "textRaw": "`multicastInterface` {string}", 714 "name": "multicastInterface", 715 "type": "string" 716 } 717 ] 718 } 719 ], 720 "desc": "<p><em>All references to scope in this section are referring to\n<a href=\"https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses\">IPv6 Zone Indices</a>, which are defined by <a href=\"https://tools.ietf.org/html/rfc4007\">RFC 4007</a>. In string form, an IP\nwith a scope index is written as <code>'IP%scope'</code> where scope is an interface name\nor interface number.</em></p>\n<p>Sets the default outgoing multicast interface of the socket to a chosen\ninterface or back to system interface selection. The <code>multicastInterface</code> must\nbe a valid string representation of an IP from the socket's family.</p>\n<p>For IPv4 sockets, this should be the IP configured for the desired physical\ninterface. All packets sent to multicast on the socket will be sent on the\ninterface determined by the most recent successful use of this call.</p>\n<p>For IPv6 sockets, <code>multicastInterface</code> should include a scope to indicate the\ninterface as in the examples that follow. In IPv6, individual <code>send</code> calls can\nalso use explicit scope in addresses, so only packets sent to a multicast\naddress without specifying an explicit scope are affected by the most recent\nsuccessful use of this call.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>\n<h4>Example: IPv6 outgoing multicast interface</h4>\n<p>On most systems, where scope format uses the interface name:</p>\n<pre><code class=\"language-js\">const socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n socket.setMulticastInterface('::%eth1');\n});\n</code></pre>\n<p>On Windows, where scope format uses an interface number:</p>\n<pre><code class=\"language-js\">const socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n socket.setMulticastInterface('::%2');\n});\n</code></pre>\n<h4>Example: IPv4 outgoing multicast interface</h4>\n<p>All systems use an IP of the host on the desired physical interface:</p>\n<pre><code class=\"language-js\">const socket = dgram.createSocket('udp4');\n\nsocket.bind(1234, () => {\n socket.setMulticastInterface('10.0.0.2');\n});\n</code></pre>", 721 "modules": [ 722 { 723 "textRaw": "Call results", 724 "name": "call_results", 725 "desc": "<p>A call on a socket that is not ready to send or no longer open may throw a <em>Not\nrunning</em> <a href=\"errors.html#class-error\"><code>Error</code></a>.</p>\n<p>If <code>multicastInterface</code> can not be parsed into an IP then an <em>EINVAL</em>\n<a href=\"errors.html#class-systemerror\"><code>System Error</code></a> is thrown.</p>\n<p>On IPv4, if <code>multicastInterface</code> is a valid address but does not match any\ninterface, or if the address does not match the family then\na <a href=\"errors.html#class-systemerror\"><code>System Error</code></a> such as <code>EADDRNOTAVAIL</code> or <code>EPROTONOSUP</code> is thrown.</p>\n<p>On IPv6, most errors with specifying or omitting scope will result in the socket\ncontinuing to use (or returning to) the system's default interface selection.</p>\n<p>A socket's address family's ANY address (IPv4 <code>'0.0.0.0'</code> or IPv6 <code>'::'</code>) can be\nused to return control of the sockets default outgoing interface to the system\nfor future multicast packets.</p>", 726 "type": "module", 727 "displayName": "Call results" 728 } 729 ] 730 }, 731 { 732 "textRaw": "`socket.setMulticastLoopback(flag)`", 733 "type": "method", 734 "name": "setMulticastLoopback", 735 "meta": { 736 "added": [ 737 "v0.3.8" 738 ], 739 "changes": [] 740 }, 741 "signatures": [ 742 { 743 "params": [ 744 { 745 "textRaw": "`flag` {boolean}", 746 "name": "flag", 747 "type": "boolean" 748 } 749 ] 750 } 751 ], 752 "desc": "<p>Sets or clears the <code>IP_MULTICAST_LOOP</code> socket option. When set to <code>true</code>,\nmulticast packets will also be received on the local interface.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>" 753 }, 754 { 755 "textRaw": "`socket.setMulticastTTL(ttl)`", 756 "type": "method", 757 "name": "setMulticastTTL", 758 "meta": { 759 "added": [ 760 "v0.3.8" 761 ], 762 "changes": [] 763 }, 764 "signatures": [ 765 { 766 "params": [ 767 { 768 "textRaw": "`ttl` {integer}", 769 "name": "ttl", 770 "type": "integer" 771 } 772 ] 773 } 774 ], 775 "desc": "<p>Sets the <code>IP_MULTICAST_TTL</code> socket option. While TTL generally stands for\n\"Time to Live\", in this context it specifies the number of IP hops that a\npacket is allowed to travel through, specifically for multicast traffic. Each\nrouter or gateway that forwards a packet decrements the TTL. If the TTL is\ndecremented to 0 by a router, it will not be forwarded.</p>\n<p>The <code>ttl</code> argument may be between 0 and 255. The default on most systems is <code>1</code>.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>" 776 }, 777 { 778 "textRaw": "`socket.setRecvBufferSize(size)`", 779 "type": "method", 780 "name": "setRecvBufferSize", 781 "meta": { 782 "added": [ 783 "v8.7.0" 784 ], 785 "changes": [] 786 }, 787 "signatures": [ 788 { 789 "params": [ 790 { 791 "textRaw": "`size` {integer}", 792 "name": "size", 793 "type": "integer" 794 } 795 ] 796 } 797 ], 798 "desc": "<p>Sets the <code>SO_RCVBUF</code> socket option. Sets the maximum socket receive buffer\nin bytes.</p>\n<p>This method throws <a href=\"errors.html#err_socket_buffer_size\"><code>ERR_SOCKET_BUFFER_SIZE</code></a> if called on an unbound socket.</p>" 799 }, 800 { 801 "textRaw": "`socket.setSendBufferSize(size)`", 802 "type": "method", 803 "name": "setSendBufferSize", 804 "meta": { 805 "added": [ 806 "v8.7.0" 807 ], 808 "changes": [] 809 }, 810 "signatures": [ 811 { 812 "params": [ 813 { 814 "textRaw": "`size` {integer}", 815 "name": "size", 816 "type": "integer" 817 } 818 ] 819 } 820 ], 821 "desc": "<p>Sets the <code>SO_SNDBUF</code> socket option. Sets the maximum socket send buffer\nin bytes.</p>\n<p>This method throws <a href=\"errors.html#err_socket_buffer_size\"><code>ERR_SOCKET_BUFFER_SIZE</code></a> if called on an unbound socket.</p>" 822 }, 823 { 824 "textRaw": "`socket.setTTL(ttl)`", 825 "type": "method", 826 "name": "setTTL", 827 "meta": { 828 "added": [ 829 "v0.1.101" 830 ], 831 "changes": [] 832 }, 833 "signatures": [ 834 { 835 "params": [ 836 { 837 "textRaw": "`ttl` {integer}", 838 "name": "ttl", 839 "type": "integer" 840 } 841 ] 842 } 843 ], 844 "desc": "<p>Sets the <code>IP_TTL</code> socket option. While TTL generally stands for \"Time to Live\",\nin this context it specifies the number of IP hops that a packet is allowed to\ntravel through. Each router or gateway that forwards a packet decrements the\nTTL. If the TTL is decremented to 0 by a router, it will not be forwarded.\nChanging TTL values is typically done for network probes or when multicasting.</p>\n<p>The <code>ttl</code> argument may be between 1 and 255. The default on most systems\nis 64.</p>\n<p>This method throws <code>EBADF</code> if called on an unbound socket.</p>" 845 }, 846 { 847 "textRaw": "`socket.unref()`", 848 "type": "method", 849 "name": "unref", 850 "meta": { 851 "added": [ 852 "v0.9.1" 853 ], 854 "changes": [] 855 }, 856 "signatures": [ 857 { 858 "return": { 859 "textRaw": "Returns: {dgram.Socket}", 860 "name": "return", 861 "type": "dgram.Socket" 862 }, 863 "params": [] 864 } 865 ], 866 "desc": "<p>By default, binding a socket will cause it to block the Node.js process from\nexiting as long as the socket is open. The <code>socket.unref()</code> method can be used\nto exclude the socket from the reference counting that keeps the Node.js\nprocess active, allowing the process to exit even if the socket is still\nlistening.</p>\n<p>Calling <code>socket.unref()</code> multiple times will have no addition effect.</p>\n<p>The <code>socket.unref()</code> method returns a reference to the socket so calls can be\nchained.</p>" 867 } 868 ] 869 } 870 ], 871 "modules": [ 872 { 873 "textRaw": "`node:dgram` module functions", 874 "name": "`node:dgram`_module_functions", 875 "methods": [ 876 { 877 "textRaw": "`dgram.createSocket(options[, callback])`", 878 "type": "method", 879 "name": "createSocket", 880 "meta": { 881 "added": [ 882 "v0.11.13" 883 ], 884 "changes": [ 885 { 886 "version": "v15.8.0", 887 "pr-url": "https://github.com/nodejs/node/pull/37026", 888 "description": "AbortSignal support was added." 889 }, 890 { 891 "version": "v11.4.0", 892 "pr-url": "https://github.com/nodejs/node/pull/23798", 893 "description": "The `ipv6Only` option is supported." 894 }, 895 { 896 "version": "v8.7.0", 897 "pr-url": "https://github.com/nodejs/node/pull/13623", 898 "description": "The `recvBufferSize` and `sendBufferSize` options are supported now." 899 }, 900 { 901 "version": "v8.6.0", 902 "pr-url": "https://github.com/nodejs/node/pull/14560", 903 "description": "The `lookup` option is supported." 904 } 905 ] 906 }, 907 "signatures": [ 908 { 909 "return": { 910 "textRaw": "Returns: {dgram.Socket}", 911 "name": "return", 912 "type": "dgram.Socket" 913 }, 914 "params": [ 915 { 916 "textRaw": "`options` {Object} Available options are:", 917 "name": "options", 918 "type": "Object", 919 "desc": "Available options are:", 920 "options": [ 921 { 922 "textRaw": "`type` {string} The family of socket. Must be either `'udp4'` or `'udp6'`. Required.", 923 "name": "type", 924 "type": "string", 925 "desc": "The family of socket. Must be either `'udp4'` or `'udp6'`. Required." 926 }, 927 { 928 "textRaw": "`reuseAddr` {boolean} When `true` [`socket.bind()`][] will reuse the address, even if another process has already bound a socket on it. **Default:** `false`.", 929 "name": "reuseAddr", 930 "type": "boolean", 931 "default": "`false`", 932 "desc": "When `true` [`socket.bind()`][] will reuse the address, even if another process has already bound a socket on it." 933 }, 934 { 935 "textRaw": "`ipv6Only` {boolean} Setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to address `::` won't make `0.0.0.0` be bound. **Default:** `false`.", 936 "name": "ipv6Only", 937 "type": "boolean", 938 "default": "`false`", 939 "desc": "Setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to address `::` won't make `0.0.0.0` be bound." 940 }, 941 { 942 "textRaw": "`recvBufferSize` {number} Sets the `SO_RCVBUF` socket value.", 943 "name": "recvBufferSize", 944 "type": "number", 945 "desc": "Sets the `SO_RCVBUF` socket value." 946 }, 947 { 948 "textRaw": "`sendBufferSize` {number} Sets the `SO_SNDBUF` socket value.", 949 "name": "sendBufferSize", 950 "type": "number", 951 "desc": "Sets the `SO_SNDBUF` socket value." 952 }, 953 { 954 "textRaw": "`lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].", 955 "name": "lookup", 956 "type": "Function", 957 "default": "[`dns.lookup()`][]", 958 "desc": "Custom lookup function." 959 }, 960 { 961 "textRaw": "`signal` {AbortSignal} An AbortSignal that may be used to close a socket.", 962 "name": "signal", 963 "type": "AbortSignal", 964 "desc": "An AbortSignal that may be used to close a socket." 965 } 966 ] 967 }, 968 { 969 "textRaw": "`callback` {Function} Attached as a listener for `'message'` events. Optional.", 970 "name": "callback", 971 "type": "Function", 972 "desc": "Attached as a listener for `'message'` events. Optional." 973 } 974 ] 975 } 976 ], 977 "desc": "<p>Creates a <code>dgram.Socket</code> object. Once the socket is created, calling\n<a href=\"#socketbindport-address-callback\"><code>socket.bind()</code></a> will instruct the socket to begin listening for datagram\nmessages. When <code>address</code> and <code>port</code> are not passed to <a href=\"#socketbindport-address-callback\"><code>socket.bind()</code></a> the\nmethod will bind the socket to the \"all interfaces\" address on a random port\n(it does the right thing for both <code>udp4</code> and <code>udp6</code> sockets). The bound address\nand port can be retrieved using <a href=\"#socketaddress\"><code>socket.address().address</code></a> and\n<a href=\"#socketaddress\"><code>socket.address().port</code></a>.</p>\n<p>If the <code>signal</code> option is enabled, calling <code>.abort()</code> on the corresponding\n<code>AbortController</code> is similar to calling <code>.close()</code> on the socket:</p>\n<pre><code class=\"language-js\">const controller = new AbortController();\nconst { signal } = controller;\nconst server = dgram.createSocket({ type: 'udp4', signal });\nserver.on('message', (msg, rinfo) => {\n console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);\n});\n// Later, when you want to close the server.\ncontroller.abort();\n</code></pre>" 978 }, 979 { 980 "textRaw": "`dgram.createSocket(type[, callback])`", 981 "type": "method", 982 "name": "createSocket", 983 "meta": { 984 "added": [ 985 "v0.1.99" 986 ], 987 "changes": [] 988 }, 989 "signatures": [ 990 { 991 "return": { 992 "textRaw": "Returns: {dgram.Socket}", 993 "name": "return", 994 "type": "dgram.Socket" 995 }, 996 "params": [ 997 { 998 "textRaw": "`type` {string} Either `'udp4'` or `'udp6'`.", 999 "name": "type", 1000 "type": "string", 1001 "desc": "Either `'udp4'` or `'udp6'`." 1002 }, 1003 { 1004 "textRaw": "`callback` {Function} Attached as a listener to `'message'` events.", 1005 "name": "callback", 1006 "type": "Function", 1007 "desc": "Attached as a listener to `'message'` events." 1008 } 1009 ] 1010 } 1011 ], 1012 "desc": "<p>Creates a <code>dgram.Socket</code> object of the specified <code>type</code>.</p>\n<p>Once the socket is created, calling <a href=\"#socketbindport-address-callback\"><code>socket.bind()</code></a> will instruct the\nsocket to begin listening for datagram messages. When <code>address</code> and <code>port</code> are\nnot passed to <a href=\"#socketbindport-address-callback\"><code>socket.bind()</code></a> the method will bind the socket to the \"all\ninterfaces\" address on a random port (it does the right thing for both <code>udp4</code>\nand <code>udp6</code> sockets). The bound address and port can be retrieved using\n<a href=\"#socketaddress\"><code>socket.address().address</code></a> and <a href=\"#socketaddress\"><code>socket.address().port</code></a>.</p>" 1013 } 1014 ], 1015 "type": "module", 1016 "displayName": "`node:dgram` module functions" 1017 } 1018 ], 1019 "type": "module", 1020 "displayName": "dgram" 1021 } 1022 ] 1023}