{"$schema":"https://doc-kit.nodejs.org/schemas/api-doc/1.0.0.json","id":"dgram","path":"/dgram","type":"module","module":"dgram","title":"UDP/datagram sockets","introducedIn":"v0.10.0","sourceLink":{"path":"lib/dgram.js","url":"https://github.com/nodejs/node/blob/HEAD/lib/dgram.js"},"stability":{"index":"2","description":"Stable"},"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"The `node:dgram` module provides an implementation of UDP datagram sockets.\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```","summary":"The `node:dgram` module provides an implementation of UDP datagram sockets.","examples":[{"language":"mjs","displayName":null,"code":"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"},{"language":"cjs","displayName":null,"code":"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"}],"children":[{"kind":"class","id":"class-dgramsocket","name":"Socket","title":"Class: `dgram.Socket`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"extends":{"text":"EventEmitter","links":[{"name":"EventEmitter","href":"events.html#class-eventemitter","start":0,"end":12}]},"description":"Encapsulates the datagram functionality.\n\nNew instances of `dgram.Socket` are created using [`dgram.createSocket()`](#dgramcreatesocketoptions-callback).\nThe `new` keyword is not to be used to create `dgram.Socket` instances.","summary":"Encapsulates the datagram functionality.","examples":[],"children":[{"kind":"event","id":"event-close","name":"close","title":"Event: `'close'`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"parameters":[],"description":"The `'close'` event is emitted after a socket is closed with [`close()`](#socketclosecallback).\nOnce triggered, no new `'message'` events will be emitted on this socket.","summary":"The `'close'` event is emitted after a socket is closed with `close()`. Once triggered, no new `'message'` events will be emitted on this socket.","examples":[],"children":[]},{"kind":"event","id":"event-connect","name":"connect","title":"Event: `'connect'`","scope":"module","overloadOf":null,"stability":null,"added":["v12.0.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"parameters":[],"description":"The `'connect'` event is emitted after a socket is associated to a remote\naddress as a result of a successful [`connect()`](#socketconnectport-address-callback) call.","summary":"The `'connect'` event is emitted after a socket is associated to a remote address as a result of a successful `connect()` call.","examples":[],"children":[]},{"kind":"event","id":"event-error","name":"error","title":"Event: `'error'`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"parameters":[{"name":"exception","type":{"text":"Error","links":[{"name":"Error","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error","start":0,"end":5}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"description":"The `'error'` event is emitted whenever any error occurs. The event handler\nfunction is passed a single `Error` object.","summary":"The `'error'` event is emitted whenever any error occurs. The event handler function is passed a single `Error` object.","examples":[],"children":[]},{"kind":"event","id":"event-listening","name":"listening","title":"Event: `'listening'`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"parameters":[],"description":"The `'listening'` event is emitted once the `dgram.Socket` is addressable and\ncan receive data. This happens either explicitly with `socket.bind()` or\nimplicitly the first time data is sent using `socket.send()`.\nUntil the `dgram.Socket` is listening, the underlying system resources do not\nexist and calls such as `socket.address()` and `socket.setTTL()` will fail.","summary":"The `'listening'` event is emitted once the `dgram.Socket` is addressable and can receive data. This happens either explicitly with `socket.bind()` or implicitly the first time data is sent using `socket.send()`. Until the `dgram.Socket` is listening, the underlying system resources do not exist and calls such as `socket.address()` and `socket.setTTL()` will fail.","examples":[],"children":[]},{"kind":"event","id":"event-message","name":"message","title":"Event: `'message'`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v18.4.0"],"prUrl":"https://github.com/nodejs/node/pull/43054","commit":null,"description":"The `family` property now returns a string instead of a number."},{"versions":["v18.0.0"],"prUrl":"https://github.com/nodejs/node/pull/41431","commit":null,"description":"The `family` property now returns a number instead of a string."}],"parameters":[{"name":"msg","type":{"text":"Buffer","links":[{"name":"Buffer","href":"buffer.html#class-buffer","start":0,"end":6}]},"description":"The message.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"rinfo","type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":"Remote address information.","default":null,"optional":false,"rest":false,"properties":[{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"The sender address.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"family","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"The address family (`'IPv4'` or `'IPv6'`).","default":null,"optional":false,"rest":false,"properties":[]},{"name":"port","type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"The sender port.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"size","type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"The message size.","default":null,"optional":false,"rest":false,"properties":[]}]}],"description":"The `'message'` event is emitted when a new datagram is available on a socket.\nThe event handler function is passed two arguments: `msg` and `rinfo`.\n\nIf the source address of the incoming packet is an IPv6 link-local\naddress, the interface name is added to the `address`. For\nexample, a packet received on the `en0` interface might have the\naddress field set to `'fe80::2618:1234:ab11:3b9c%en0'`, where `'%en0'`\nis the interface name as a zone ID suffix.","summary":"The `'message'` event is emitted when a new datagram is available on a socket. The event handler function is passed two arguments: `msg` and `rinfo`.","examples":[],"children":[]},{"kind":"method","id":"socketaddmembershipmulticastaddress-multicastinterface","name":"addMembership","title":"`socket.addMembership(multicastAddress[, multicastInterface])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.6.9"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"multicastAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"multicastInterface","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Tells the kernel to join a multicast group at the given `multicastAddress` and\n`multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If the\n`multicastInterface` 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 `addMembership` multiple times, once per interface.\n\nWhen called on an unbound socket, this method will implicitly bind to a random\nport, listening on all interfaces.\n\nWhen sharing a UDP socket across multiple `cluster` workers, the\n`socket.addMembership()` function must be called only once or an\n`EADDRINUSE` error will occur:\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```","summary":"Tells the kernel to join a multicast group at the given `multicastAddress` and `multicastInterface` using the `IP_ADD_MEMBERSHIP` socket option. If the `multicastInterface` argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call `addMembership` multiple times, once per interface.","examples":[{"language":"mjs","displayName":null,"code":"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}"},{"language":"cjs","displayName":null,"code":"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}"}],"children":[]},{"kind":"method","id":"socketaddsourcespecificmembershipsourceaddress-groupaddress-multicastinterface","name":"addSourceSpecificMembership","title":"`socket.addSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`","scope":"module","overloadOf":null,"stability":null,"added":["v13.1.0","v12.16.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"sourceAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"groupAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"multicastInterface","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Tells the kernel to join a source-specific multicast channel at the given\n`sourceAddress` and `groupAddress`, using the `multicastInterface` with the\n`IP_ADD_SOURCE_MEMBERSHIP` socket option. If the `multicastInterface` 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`socket.addSourceSpecificMembership()` multiple times, once per interface.\n\nWhen called on an unbound socket, this method will implicitly bind to a random\nport, listening on all interfaces.","summary":"Tells the kernel to join a source-specific multicast channel at the given `sourceAddress` and `groupAddress`, using the `multicastInterface` with the `IP_ADD_SOURCE_MEMBERSHIP` socket option. If the `multicastInterface` argument is not specified, the operating system will choose one interface and will add membership to it. To add membership to every available interface, call `socket.addSourceSpecificMembership()` multiple times, once per interface.","examples":[],"children":[]},{"kind":"method","id":"socketaddress","name":"address","title":"`socket.address()`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":""}},"description":"Returns an object containing the address information for a socket.\nFor UDP sockets, this object will contain `address`, `family`, and `port`\nproperties.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"Returns an object containing the address information for a socket. For UDP sockets, this object will contain `address`, `family`, and `port` properties.","examples":[],"children":[]},{"kind":"method","id":"socketbindport-address-callback","name":"bind","title":"`socket.bind([port][, address][, callback])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v0.9.1"],"prUrl":null,"commit":"332fea5ac1816e498030109c4211bca24a7fa667","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."}],"signature":{"parameters":[{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"with no parameters. Called when binding is complete.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"For UDP sockets, causes the `dgram.Socket` to listen for datagram\nmessages on a named `port` and optional `address`. If `port` is not\nspecified or is `0`, the operating system will attempt to bind to a\nrandom port. If `address` is not specified, the operating system will\nattempt to listen on all addresses. Once binding is complete, a\n`'listening'` event is emitted and the optional `callback` function is\ncalled.\n\nSpecifying both a `'listening'` event listener and passing a\n`callback` to the `socket.bind()` method is not harmful but not very\nuseful.\n\nA bound datagram socket keeps the Node.js process running to receive\ndatagram messages.\n\nIf binding fails, an `'error'` event is generated. In rare cases (e.g.,\nattempting to bind with a closed socket), an [`Error`](errors.html#class-error) may be thrown.\n\nExample of a UDP server listening on port 41234:\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```","summary":"For UDP sockets, causes the `dgram.Socket` to listen for datagram messages on a named `port` and optional `address`. If `port` is not specified or is `0`, the operating system will attempt to bind to a random port. If `address` is not specified, the operating system will attempt to listen on all addresses. Once binding is complete, a `'listening'` event is emitted and the optional `callback` function is called.","examples":[{"language":"mjs","displayName":null,"code":"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"},{"language":"cjs","displayName":null,"code":"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"}],"children":[]},{"kind":"method","id":"socketbindoptions-callback","name":"bind","title":"`socket.bind(options[, callback])`","scope":"module","overloadOf":"socketbindport-address-callback","stability":null,"added":["v0.11.14"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"options","type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":"Required. Supports the following properties:","default":null,"optional":false,"rest":false,"properties":[{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"exclusive","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"fd","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"For UDP sockets, causes the `dgram.Socket` to listen for datagram\nmessages on a named `port` and optional `address` that are passed as\nproperties of an `options` object passed as the first argument. If\n`port` is not specified or is `0`, the operating system will attempt\nto bind to a random port. If `address` is not specified, the operating\nsystem will attempt to listen on all addresses. Once binding is\ncomplete, a `'listening'` event is emitted and the optional `callback`\nfunction is called.\n\nThe `options` object may contain a `fd` property. When a `fd` greater\nthan `0` is set, it will wrap around an existing socket with the given\nfile descriptor. In this case, the properties of `port` and `address`\nwill be ignored.\n\nSpecifying both a `'listening'` event listener and passing a\n`callback` to the `socket.bind()` method is not harmful but not very\nuseful.\n\nThe `options` object may contain an additional `exclusive` property that is\nused when using `dgram.Socket` objects with the [`cluster`](cluster.html) module. When\n`exclusive` is set to `false` (the default), cluster workers will use the same\nunderlying socket handle allowing connection handling duties to be shared.\nWhen `exclusive` is `true`, however, the handle is not shared and attempted\nport sharing results in an error. Creating a `dgram.Socket` with the `reusePort`\noption set to `true` causes `exclusive` to always be `true` when `socket.bind()`\nis called.\n\nA bound datagram socket keeps the Node.js process running to receive\ndatagram messages.\n\nIf binding fails, an `'error'` event is generated. In rare cases (e.g.,\nattempting to bind with a closed socket), an [`Error`](errors.html#class-error) may be thrown.\n\nAn example socket listening on an exclusive port is shown below.\n\n```js\nsocket.bind({\n  address: 'localhost',\n  port: 8000,\n  exclusive: true,\n});\n```","summary":"For UDP sockets, causes the `dgram.Socket` to listen for datagram messages on a named `port` and optional `address` that are passed as properties of an `options` object passed as the first argument. If `port` is not specified or is `0`, the operating system will attempt to bind to a random port. If `address` is not specified, the operating system will attempt to listen on all addresses. Once binding is complete, a `'listening'` event is emitted and the optional `callback` function is called.","examples":[{"language":"js","displayName":null,"code":"socket.bind({\n  address: 'localhost',\n  port: 8000,\n  exclusive: true,\n});"}],"children":[]},{"kind":"method","id":"socketbindsyncoptions","name":"bindSync","title":"`socket.bindSync([options])`","scope":"module","overloadOf":null,"stability":null,"added":["v26.4.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"options","type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"If omitted or `0`, the operating system will assign an\narbitrary unused port.","default":"0","optional":true,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"A numeric IP address to bind to. Unlike\n[`socket.bind()`](#socketbindport-address-callback), no DNS resolution is performed, so a host name is not\naccepted. If omitted, the operating system binds to all addresses\n(`'0.0.0.0'` for `udp4` sockets, `'::'` for `udp6`).","default":null,"optional":false,"rest":false,"properties":[]}]}],"returns":{"type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":"The bound address as returned by [`socket.address()`](#socketaddress)."}},"description":"The synchronous counterpart of [`socket.bind()`](#socketbindport-address-callback). `bind(2)` is a local,\nnon-blocking system call, so the bind is performed inline and the resolved\naddress is returned immediately, including the operating-system-assigned\nephemeral port when `port` is `0`:\n\n```js\nconst dgram = require('node:dgram');\n\nconst socket = dgram.createSocket('udp4');\nconst address = socket.bindSync({ address: '0.0.0.0', port: 0 });\nconsole.log(address); // e.g. { address: '0.0.0.0', family: 'IPv4', port: 53124 }\n```\n\nA bind failure such as `EADDRINUSE` is thrown synchronously rather than emitted\nas an `'error'` event. After `bindSync()` returns, [`socket.address()`](#socketaddress) is\nvalid synchronously and the `'listening'` event is emitted on the next tick.\n\n`address` must be a numeric IP literal; `bindSync()` never performs DNS\nresolution (asynchronous name resolution being the only genuinely blocking part\nof binding). Incoming datagrams continue to be delivered asynchronously via the\n[`'message'`](#event-message) event. `bindSync()` always binds the socket's own handle and\ndoes not participate in [`cluster`](cluster.html) handle sharing.","summary":"The synchronous counterpart of `socket.bind()`. `bind(2)` is a local, non-blocking system call, so the bind is performed inline and the resolved address is returned immediately, including the operating-system-assigned ephemeral port when `port` is `0`:","examples":[{"language":"js","displayName":null,"code":"const dgram = require('node:dgram');\n\nconst socket = dgram.createSocket('udp4');\nconst address = socket.bindSync({ address: '0.0.0.0', port: 0 });\nconsole.log(address); // e.g. { address: '0.0.0.0', family: 'IPv4', port: 53124 }"}],"children":[]},{"kind":"method","id":"socketclosecallback","name":"close","title":"`socket.close([callback])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Called when the socket has been closed.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Close the underlying socket and stop listening for data on it. If a callback is\nprovided, it is added as a listener for the [`'close'`](#event-close) event.","summary":"Close the underlying socket and stop listening for data on it. If a callback is provided, it is added as a listener for the `'close'` event.","examples":[],"children":[]},{"kind":"method","id":"socketsymbolasyncdispose","name":"[Symbol.asyncDispose]","title":"`socket[Symbol.asyncDispose]()`","scope":"module","overloadOf":null,"stability":null,"added":["v20.5.0","v18.18.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v24.2.0"],"prUrl":"https://github.com/nodejs/node/pull/58467","commit":null,"description":"No longer experimental."}],"signature":{"parameters":[],"returns":null},"description":"Calls [`socket.close()`](#socketclosecallback) and returns a promise that fulfills when the\nsocket has closed.","summary":"Calls `socket.close()` and returns a promise that fulfills when the socket has closed.","examples":[],"children":[]},{"kind":"method","id":"socketconnectport-address-callback","name":"connect","title":"`socket.connect(port[, address][, callback])`","scope":"module","overloadOf":null,"stability":null,"added":["v12.0.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Called when the connection is completed or on error.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Associates the `dgram.Socket` 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 `connect()` on an already connected socket will result\nin an [`ERR_SOCKET_DGRAM_IS_CONNECTED`](errors.html#err_socket_dgram_is_connected) exception. If `address` is not\nprovided, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for `udp6` sockets)\nwill be used by default. Once the connection is complete, a `'connect'` event\nis emitted and the optional `callback` function is called. In case of failure,\nthe `callback` is called or, failing this, an `'error'` event is emitted.","summary":"Associates the `dgram.Socket` to a remote address and port. Every message sent by this handle is automatically sent to that destination. Also, the socket will only receive messages from that remote peer. Trying to call `connect()` on an already connected socket will result in an `ERR_SOCKET_DGRAM_IS_CONNECTED` exception. If `address` is not provided, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for `udp6` sockets) will be used by default. Once the connection is complete, a `'connect'` event is emitted and the optional `callback` function is called. In case of failure, the `callback` is called or, failing this, an `'error'` event is emitted.","examples":[],"children":[]},{"kind":"method","id":"socketconnectsyncport-address","name":"connectSync","title":"`socket.connectSync(port[, address])`","scope":"module","overloadOf":null,"stability":null,"added":["v26.4.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"A numeric IP address to connect to. Unlike\n[`socket.connect()`](#socketconnectport-address-callback), no DNS resolution is performed, so a host name is not\naccepted. If omitted, `'127.0.0.1'` (for `udp4` sockets) or `'::1'` (for\n`udp6` sockets) is used.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"The synchronous counterpart of [`socket.connect()`](#socketconnectport-address-callback). For a UDP socket\n`connect(2)` only records the default peer address and is a local, non-blocking\nsystem call, so the association is performed inline. Any error raised by the\ncall itself (for example `EAFNOSUPPORT` for a mismatched address family) is\nthrown synchronously rather than reported via the `'error'` event. Because\n`connect(2)` does not probe reachability, errors such as `ECONNREFUSED` are\nstill surfaced asynchronously on a later send or receive, exactly as for\n[`socket.connect()`](#socketconnectport-address-callback):\n\n```js\nconst dgram = require('node:dgram');\n\nconst socket = dgram.createSocket('udp4');\nsocket.connectSync(41234, '127.0.0.1');\nconsole.log(socket.remoteAddress()); // { address: '127.0.0.1', family: 'IPv4', port: 41234 }\n```\n\nIf the socket is still unbound it is bound synchronously first. After\n`connectSync()` returns, [`socket.remoteAddress()`](#socketremoteaddress) is valid synchronously\nand the `'connect'` event is emitted on the next tick. Trying to call\n`connectSync()` on an already connected socket throws an\n[`ERR_SOCKET_DGRAM_IS_CONNECTED`](errors.html#err_socket_dgram_is_connected) exception, and calling it while an\nasynchronous [`socket.bind()`](#socketbindport-address-callback) is still in progress throws an\n[`ERR_SOCKET_ALREADY_BOUND`](errors.html#err_socket_already_bound) exception.\n\n`address` must be a numeric IP literal; `connectSync()` never performs DNS\nresolution (asynchronous name resolution being the only genuinely blocking part\nof connecting).","summary":"The synchronous counterpart of `socket.connect()`. For a UDP socket `connect(2)` only records the default peer address and is a local, non-blocking system call, so the association is performed inline. Any error raised by the call itself (for example `EAFNOSUPPORT` for a mismatched address family) is thrown synchronously rather than reported via the `'error'` event. Because `connect(2)` does not probe reachability, errors such as `ECONNREFUSED` are still surfaced asynchronously on a later send or receive, exactly as for `socket.connect()`:","examples":[{"language":"js","displayName":null,"code":"const dgram = require('node:dgram');\n\nconst socket = dgram.createSocket('udp4');\nsocket.connectSync(41234, '127.0.0.1');\nconsole.log(socket.remoteAddress()); // { address: '127.0.0.1', family: 'IPv4', port: 41234 }"}],"children":[]},{"kind":"method","id":"socketdisconnect","name":"disconnect","title":"`socket.disconnect()`","scope":"module","overloadOf":null,"stability":null,"added":["v12.0.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":null},"description":"A synchronous function that disassociates a connected `dgram.Socket` from\nits remote address. Trying to call `disconnect()` on an unbound or already\ndisconnected socket will result in an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`](errors.html#err_socket_dgram_not_connected)\nexception.","summary":"A synchronous function that disassociates a connected `dgram.Socket` from its remote address. Trying to call `disconnect()` on an unbound or already disconnected socket will result in an `ERR_SOCKET_DGRAM_NOT_CONNECTED` exception.","examples":[],"children":[]},{"kind":"method","id":"socketdropmembershipmulticastaddress-multicastinterface","name":"dropMembership","title":"`socket.dropMembership(multicastAddress[, multicastInterface])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.6.9"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"multicastAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"multicastInterface","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Instructs the kernel to leave a multicast group at `multicastAddress` using the\n`IP_DROP_MEMBERSHIP` 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.\n\nIf `multicastInterface` is not specified, the operating system will attempt to\ndrop membership on all valid interfaces.","summary":"Instructs the kernel to leave a multicast group at `multicastAddress` using the `IP_DROP_MEMBERSHIP` socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.","examples":[],"children":[]},{"kind":"method","id":"socketdropsourcespecificmembershipsourceaddress-groupaddress-multicastinterface","name":"dropSourceSpecificMembership","title":"`socket.dropSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])`","scope":"module","overloadOf":null,"stability":null,"added":["v13.1.0","v12.16.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"sourceAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"groupAddress","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]},{"name":"multicastInterface","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Instructs the kernel to leave a source-specific multicast channel at the given\n`sourceAddress` and `groupAddress` using the `IP_DROP_SOURCE_MEMBERSHIP`\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.\n\nIf `multicastInterface` is not specified, the operating system will attempt to\ndrop membership on all valid interfaces.","summary":"Instructs the kernel to leave a source-specific multicast channel at the given `sourceAddress` and `groupAddress` using the `IP_DROP_SOURCE_MEMBERSHIP` socket option. This method is automatically called by the kernel when the socket is closed or the process terminates, so most apps will never have reason to call this.","examples":[],"children":[]},{"kind":"method","id":"socketgetrecvbuffersize","name":"getRecvBufferSize","title":"`socket.getRecvBufferSize()`","scope":"module","overloadOf":null,"stability":null,"added":["v8.7.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"the `SO_RCVBUF` socket receive buffer size in bytes."}},"description":"This method throws [`ERR_SOCKET_BUFFER_SIZE`](errors.html#err_socket_buffer_size) if called on an unbound socket.","summary":"This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket.","examples":[],"children":[]},{"kind":"method","id":"socketgetsendbuffersize","name":"getSendBufferSize","title":"`socket.getSendBufferSize()`","scope":"module","overloadOf":null,"stability":null,"added":["v8.7.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"the `SO_SNDBUF` socket send buffer size in bytes."}},"description":"This method throws [`ERR_SOCKET_BUFFER_SIZE`](errors.html#err_socket_buffer_size) if called on an unbound socket.","summary":"This method throws `ERR_SOCKET_BUFFER_SIZE` if called on an unbound socket.","examples":[],"children":[]},{"kind":"method","id":"socketgetsendqueuesize","name":"getSendQueueSize","title":"`socket.getSendQueueSize()`","scope":"module","overloadOf":null,"stability":null,"added":["v18.8.0","v16.19.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"Number of bytes queued for sending."}},"description":"","summary":"","examples":[],"children":[]},{"kind":"method","id":"socketgetsendqueuecount","name":"getSendQueueCount","title":"`socket.getSendQueueCount()`","scope":"module","overloadOf":null,"stability":null,"added":["v18.8.0","v16.19.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"Number of send requests currently in the queue awaiting\nto be processed."}},"description":"","summary":"","examples":[],"children":[]},{"kind":"method","id":"socketref","name":"ref","title":"`socket.ref()`","scope":"module","overloadOf":null,"stability":null,"added":["v0.9.1"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"dgram.Socket","links":[{"name":"dgram.Socket","href":"dgram.html#class-dgramsocket","start":0,"end":12}]},"description":""}},"description":"By default, binding a socket will cause it to block the Node.js process from\nexiting as long as the socket is open. The `socket.unref()` method can be used\nto exclude the socket from the reference counting that keeps the Node.js\nprocess active. The `socket.ref()` method adds the socket back to the reference\ncounting and restores the default behavior.\n\nCalling `socket.ref()` multiple times will have no additional effect.\n\nThe `socket.ref()` method returns a reference to the socket so calls can be\nchained.","summary":"By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The `socket.unref()` method can be used to exclude the socket from the reference counting that keeps the Node.js process active. The `socket.ref()` method adds the socket back to the reference counting and restores the default behavior.","examples":[],"children":[]},{"kind":"method","id":"socketremoteaddress","name":"remoteAddress","title":"`socket.remoteAddress()`","scope":"module","overloadOf":null,"stability":null,"added":["v12.0.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":""}},"description":"Returns an object containing the `address`, `family`, and `port` of the remote\nendpoint. This method throws an [`ERR_SOCKET_DGRAM_NOT_CONNECTED`](errors.html#err_socket_dgram_not_connected) exception\nif the socket is not connected.","summary":"Returns an object containing the `address`, `family`, and `port` of the remote endpoint. This method throws an `ERR_SOCKET_DGRAM_NOT_CONNECTED` exception if the socket is not connected.","examples":[],"children":[]},{"kind":"method","id":"socketsendmsg-offset-length-port-address-callback","name":"send","title":"`socket.send(msg[, offset, length][, port][, address][, callback])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v17.0.0"],"prUrl":"https://github.com/nodejs/node/pull/39190","commit":null,"description":"The `address` parameter now only accepts a `string`, `null` or `undefined`."},{"versions":["v14.5.0","v12.19.0"],"prUrl":"https://github.com/nodejs/node/pull/22413","commit":null,"description":"The `msg` parameter can now be any `TypedArray` or `DataView`."},{"versions":["v12.0.0"],"prUrl":"https://github.com/nodejs/node/pull/26871","commit":null,"description":"Added support for sending data on connected sockets."},{"versions":["v8.0.0"],"prUrl":"https://github.com/nodejs/node/pull/11985","commit":null,"description":"The `msg` parameter can be an `Uint8Array` now."},{"versions":["v8.0.0"],"prUrl":"https://github.com/nodejs/node/pull/10473","commit":null,"description":"The `address` parameter is always optional now."},{"versions":["v6.0.0"],"prUrl":"https://github.com/nodejs/node/pull/5929","commit":null,"description":"On success, `callback` will now be called with an `error` argument of `null` rather than `0`."},{"versions":["v5.7.0"],"prUrl":"https://github.com/nodejs/node/pull/4374","commit":null,"description":"The `msg` parameter can be an array now. Also, the `offset` and `length` parameters are optional now."}],"signature":{"parameters":[{"name":"msg","type":{"text":"Buffer | TypedArray | DataView | string | Array","links":[{"name":"Buffer","href":"buffer.html#class-buffer","start":0,"end":6},{"name":"TypedArray","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray","start":9,"end":19},{"name":"DataView","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/DataView","start":22,"end":30},{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":33,"end":39},{"name":"Array","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array","start":42,"end":47}]},"description":"Message to be sent.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"offset","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"Offset in the buffer where the message starts.","default":null,"optional":true,"rest":false,"properties":[]},{"name":"length","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"Number of bytes in the message.","default":null,"optional":true,"rest":false,"properties":[]},{"name":"port","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"Destination port.","default":null,"optional":true,"rest":false,"properties":[]},{"name":"address","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"Destination host name or IP address.","default":null,"optional":true,"rest":false,"properties":[]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Called when the message has been sent.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":null},"description":"Broadcasts a datagram on the socket.\nFor connectionless sockets, the destination `port` and `address` must be\nspecified. Connected sockets, on the other hand, will use their associated\nremote endpoint, so the `port` and `address` arguments must not be set.\n\nThe `msg` argument contains the message to be sent.\nDepending on its type, different behavior can apply. If `msg` is a `Buffer`,\nany `TypedArray` or a `DataView`,\nthe `offset` and `length` specify the offset within the `Buffer` where the\nmessage begins and the number of bytes in the message, respectively.\nIf `msg` is a `String`, then it is automatically converted to a `Buffer`\nwith `'utf8'` encoding. With messages that\ncontain multi-byte characters, `offset` and `length` will be calculated with\nrespect to [byte length](buffer.html#static-method-bufferbytelengthstring-encoding) and not the character position.\nIf `msg` is an array, `offset` and `length` must not be specified.\n\nThe `address` argument is a string. If the value of `address` is a host name,\nDNS will be used to resolve the address of the host. If `address` is not\nprovided or otherwise nullish, `'127.0.0.1'` (for `udp4` sockets) or `'::1'`\n(for `udp6` sockets) will be used by default.\n\nIf the socket has not been previously bound with a call to `bind`, the socket\nis assigned a random port number and is bound to the \"all interfaces\" address\n(`'0.0.0.0'` for `udp4` sockets, `'::0'` for `udp6` sockets.)\n\nAn optional `callback` function may be specified to as a way of reporting\nDNS errors or for determining when it is safe to reuse the `buf` object.\nDNS lookups delay the time to send for at least one tick of the\nNode.js event loop.\n\nThe only way to know for sure that the datagram has been sent is by using a\n`callback`. If an error occurs and a `callback` is given, the error will be\npassed as the first argument to the `callback`. If a `callback` is not given,\nthe error is emitted as an `'error'` event on the `socket` object.\n\nOffset and length are optional but both *must* be set if either are used.\nThey are supported only when the first argument is a `Buffer`, a `TypedArray`,\nor a `DataView`.\n\nThis method throws [`ERR_SOCKET_BAD_PORT`](errors.html#err_socket_bad_port) if called on an unbound socket.\n\nExample of sending a UDP packet to a port on `localhost`;\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```\n\nExample of sending a UDP packet composed of multiple buffers to a port on\n`127.0.0.1`;\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```\n\nSending 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.\n\nExample of sending a UDP packet using a socket connected to a port on\n`localhost`:\n\n```mjs\nimport 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```\n\n```cjs\nconst 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```","summary":"Broadcasts a datagram on the socket. For connectionless sockets, the destination `port` and `address` must be specified. Connected sockets, on the other hand, will use their associated remote endpoint, so the `port` and `address` arguments must not be set.","examples":[{"language":"mjs","displayName":null,"code":"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});"},{"language":"cjs","displayName":null,"code":"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});"},{"language":"mjs","displayName":null,"code":"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});"},{"language":"cjs","displayName":null,"code":"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});"},{"language":"mjs","displayName":null,"code":"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});"},{"language":"cjs","displayName":null,"code":"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});"}],"children":[{"kind":"section","id":"note-about-udp-datagram-size","name":"Note about UDP datagram size","title":"Note about UDP datagram size","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"The maximum size of an IPv4/v6 datagram depends on the `MTU`\n(Maximum Transmission Unit) and on the `Payload Length` field size.\n\n* The `Payload Length` field is 16 bits wide, which means that a normal\n  payload cannot exceed 64K octets including the internet header and data\n  (65,507 bytes = 65,535 − 8 bytes UDP header − 20 bytes IP header);\n  this is generally true for loopback interfaces, but such long datagram\n  messages are impractical for most hosts and networks.\n\n* The `MTU` is the largest size a given link layer technology can support for\n  datagram messages. For any link, IPv4 mandates a minimum `MTU` of 68\n  octets, while the recommended `MTU` for IPv4 is 576 (typically recommended\n  as the `MTU` for dial-up type applications), whether they arrive whole or in\n  fragments.\n\n  For IPv6, the minimum `MTU` is 1280 octets. However, the mandatory minimum\n  fragment reassembly buffer size is 1500 octets. The value of 68 octets is\n  very small, since most current link layer technologies, like Ethernet, have a\n  minimum `MTU` of 1500.\n\nIt is impossible to know in advance the MTU of each link through which\na packet might travel. Sending a datagram greater than the receiver `MTU` will\nnot work because the packet will get silently dropped without informing the\nsource that the data did not reach its intended recipient.","summary":"The maximum size of an IPv4/v6 datagram depends on the `MTU` (Maximum Transmission Unit) and on the `Payload Length` field size.","examples":[],"children":[]}]},{"kind":"method","id":"socketsetbroadcastflag","name":"setBroadcast","title":"`socket.setBroadcast(flag)`","scope":"module","overloadOf":null,"stability":null,"added":["v0.6.9"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"flag","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP\npackets may be sent to a local interface's broadcast address.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"Sets or clears the `SO_BROADCAST` socket option. When set to `true`, UDP packets may be sent to a local interface's broadcast address.","examples":[],"children":[]},{"kind":"method","id":"socketsetmulticastinterfacemulticastinterface","name":"setMulticastInterface","title":"`socket.setMulticastInterface(multicastInterface)`","scope":"module","overloadOf":null,"stability":null,"added":["v8.6.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"multicastInterface","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"*All references to scope in this section are referring to\n[IPv6 Zone Indexes](https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses), which are defined by [RFC 4007](https://tools.ietf.org/html/rfc4007). In string form, an IP\nwith a scope index is written as `'IP%scope'` where scope is an interface name\nor interface number.*\n\nSets the default outgoing multicast interface of the socket to a chosen\ninterface or back to system interface selection. The `multicastInterface` must\nbe a valid string representation of an IP from the socket's family.\n\nFor 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.\n\nFor IPv6 sockets, `multicastInterface` should include a scope to indicate the\ninterface as in the examples that follow. In IPv6, individual `send` 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.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"_All references to scope in this section are referring to IPv6 Zone Indexes, which are defined by RFC 4007. In string form, an IP with a scope index is written as `'IP%scope'` where scope is an interface name or interface number._","examples":[],"children":[{"kind":"section","id":"example-ipv6-outgoing-multicast-interface","name":"Example: IPv6 outgoing multicast interface","title":"Example: IPv6 outgoing multicast interface","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"On most systems, where scope format uses the interface name:\n\n```js\nconst socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('::%eth1');\n});\n```\n\nOn Windows, where scope format uses an interface number:\n\n```js\nconst socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('::%2');\n});\n```","summary":"On most systems, where scope format uses the interface name:","examples":[{"language":"js","displayName":null,"code":"const socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('::%eth1');\n});"},{"language":"js","displayName":null,"code":"const socket = dgram.createSocket('udp6');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('::%2');\n});"}],"children":[]},{"kind":"section","id":"example-ipv4-outgoing-multicast-interface","name":"Example: IPv4 outgoing multicast interface","title":"Example: IPv4 outgoing multicast interface","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"All systems use an IP of the host on the desired physical interface:\n\n```js\nconst socket = dgram.createSocket('udp4');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('10.0.0.2');\n});\n```","summary":"All systems use an IP of the host on the desired physical interface:","examples":[{"language":"js","displayName":null,"code":"const socket = dgram.createSocket('udp4');\n\nsocket.bind(1234, () => {\n  socket.setMulticastInterface('10.0.0.2');\n});"}],"children":[]},{"kind":"section","id":"call-results","name":"Call results","title":"Call results","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"A call on a socket that is not ready to send or no longer open may throw a *Not\nrunning* [`Error`](errors.html#class-error).\n\nIf `multicastInterface` cannot be parsed into an IP then an *EINVAL*\n[`System Error`](errors.html#class-systemerror) is thrown.\n\nOn IPv4, if `multicastInterface` is a valid address but does not match any\ninterface, or if the address does not match the family then\na [`System Error`](errors.html#class-systemerror) such as `EADDRNOTAVAIL` or `EPROTONOSUP` is thrown.\n\nOn 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.\n\nA socket's address family's ANY address (IPv4 `'0.0.0.0'` or IPv6 `'::'`) can be\nused to return control of the sockets default outgoing interface to the system\nfor future multicast packets.","summary":"A call on a socket that is not ready to send or no longer open may throw a _Not running_ `Error`.","examples":[],"children":[]}]},{"kind":"method","id":"socketsetmulticastloopbackflag","name":"setMulticastLoopback","title":"`socket.setMulticastLoopback(flag)`","scope":"module","overloadOf":null,"stability":null,"added":["v0.3.8"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"flag","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets or clears the `IP_MULTICAST_LOOP` socket option. When set to `true`,\nmulticast packets will also be received on the local interface.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"Sets or clears the `IP_MULTICAST_LOOP` socket option. When set to `true`, multicast packets will also be received on the local interface.","examples":[],"children":[]},{"kind":"method","id":"socketsetmulticastttlttl","name":"setMulticastTTL","title":"`socket.setMulticastTTL(ttl)`","scope":"module","overloadOf":null,"stability":null,"added":["v0.3.8"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"ttl","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets the `IP_MULTICAST_TTL` 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.\n\nThe `ttl` argument may be between 0 and 255. The default on most systems is `1`.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"Sets the `IP_MULTICAST_TTL` socket option. While TTL generally stands for \"Time to Live\", in this context it specifies the number of IP hops that a packet is allowed to travel through, specifically for multicast traffic. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded.","examples":[],"children":[]},{"kind":"method","id":"socketsetrecvbuffersizesize","name":"setRecvBufferSize","title":"`socket.setRecvBufferSize(size)`","scope":"module","overloadOf":null,"stability":null,"added":["v8.7.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"size","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets the `SO_RCVBUF` socket option. Sets the maximum socket receive buffer\nin bytes.\n\nThis method throws [`ERR_SOCKET_BUFFER_SIZE`](errors.html#err_socket_buffer_size) if called on an unbound socket.","summary":"Sets the `SO_RCVBUF` socket option. Sets the maximum socket receive buffer in bytes.","examples":[],"children":[]},{"kind":"method","id":"socketsetsendbuffersizesize","name":"setSendBufferSize","title":"`socket.setSendBufferSize(size)`","scope":"module","overloadOf":null,"stability":null,"added":["v8.7.0"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"size","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets the `SO_SNDBUF` socket option. Sets the maximum socket send buffer\nin bytes.\n\nThis method throws [`ERR_SOCKET_BUFFER_SIZE`](errors.html#err_socket_buffer_size) if called on an unbound socket.","summary":"Sets the `SO_SNDBUF` socket option. Sets the maximum socket send buffer in bytes.","examples":[],"children":[]},{"kind":"method","id":"socketsetttlttl","name":"setTTL","title":"`socket.setTTL(ttl)`","scope":"module","overloadOf":null,"stability":null,"added":["v0.1.101"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"ttl","type":{"text":"integer","links":[{"name":"integer","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":7}]},"description":"","default":null,"optional":false,"rest":false,"properties":[]}],"returns":null},"description":"Sets the `IP_TTL` 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.\n\nThe `ttl` argument may be between 1 and 255. The default on most systems\nis 64.\n\nThis method throws `EBADF` if called on an unbound socket.","summary":"Sets the `IP_TTL` socket option. While TTL generally stands for \"Time to Live\", in this context it specifies the number of IP hops that a packet is allowed to travel through. Each router or gateway that forwards a packet decrements the TTL. If the TTL is decremented to 0 by a router, it will not be forwarded. Changing TTL values is typically done for network probes or when multicasting.","examples":[],"children":[]},{"kind":"method","id":"socketunref","name":"unref","title":"`socket.unref()`","scope":"module","overloadOf":null,"stability":null,"added":["v0.9.1"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[],"returns":{"type":{"text":"dgram.Socket","links":[{"name":"dgram.Socket","href":"dgram.html#class-dgramsocket","start":0,"end":12}]},"description":""}},"description":"By default, binding a socket will cause it to block the Node.js process from\nexiting as long as the socket is open. The `socket.unref()` 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.\n\nCalling `socket.unref()` multiple times will have no additional effect.\n\nThe `socket.unref()` method returns a reference to the socket so calls can be\nchained.","summary":"By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open. The `socket.unref()` method can be used to exclude the socket from the reference counting that keeps the Node.js process active, allowing the process to exit even if the socket is still listening.","examples":[],"children":[]}]},{"kind":"section","id":"nodedgram-module-functions","name":"node:dgram module functions","title":"`node:dgram` module functions","scope":"module","overloadOf":null,"stability":null,"added":[],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"description":"","summary":"","examples":[],"children":[{"kind":"method","id":"dgramcreatesocketoptions-callback","name":"createSocket","title":"`dgram.createSocket(options[, callback])`","scope":"module","overloadOf":null,"stability":null,"added":["v0.11.13"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[{"versions":["v23.1.0","v22.12.0"],"prUrl":"https://github.com/nodejs/node/pull/55403","commit":null,"description":"The `reusePort` option is supported."},{"versions":["v15.8.0"],"prUrl":"https://github.com/nodejs/node/pull/37026","commit":null,"description":"AbortSignal support was added."},{"versions":["v11.4.0"],"prUrl":"https://github.com/nodejs/node/pull/23798","commit":null,"description":"The `ipv6Only` option is supported."},{"versions":["v8.7.0"],"prUrl":"https://github.com/nodejs/node/pull/13623","commit":null,"description":"The `recvBufferSize` and `sendBufferSize` options are supported now."},{"versions":["v8.6.0"],"prUrl":"https://github.com/nodejs/node/pull/14560","commit":null,"description":"The `lookup` option is supported."}],"signature":{"parameters":[{"name":"options","type":{"text":"Object","links":[{"name":"Object","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object","start":0,"end":6}]},"description":"Available options are:","default":null,"optional":false,"rest":false,"properties":[{"name":"type","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"The family of socket. Must be either `'udp4'` or `'udp6'`.\nRequired.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"reuseAddr","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"When `true` [`socket.bind()`](#socketbindport-address-callback) will reuse the\naddress, even if another process has already bound a socket on it, but\nonly one socket can receive the data.","default":"false","optional":true,"rest":false,"properties":[]},{"name":"reusePort","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"When `true` [`socket.bind()`](#socketbindport-address-callback) will reuse the\nport, even if another process has already bound a socket on it. Incoming\ndatagrams are distributed to listening sockets. The option is available\nonly on some platforms, such as Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+,\nSolaris 11.4, and AIX 7.2.5+. On unsupported platforms, this option raises\nan error when the socket is bound.","default":"false","optional":true,"rest":false,"properties":[]},{"name":"ipv6Only","type":{"text":"boolean","links":[{"name":"boolean","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#boolean_type","start":0,"end":7}]},"description":"Setting `ipv6Only` to `true` will\ndisable dual-stack support, i.e., binding to address `::` won't make\n`0.0.0.0` be bound.","default":"false","optional":true,"rest":false,"properties":[]},{"name":"recvBufferSize","type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"Sets the `SO_RCVBUF` socket value.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"sendBufferSize","type":{"text":"number","links":[{"name":"number","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type","start":0,"end":6}]},"description":"Sets the `SO_SNDBUF` socket value.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"lookup","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Custom lookup function.","default":"dns.lookup()`. When the default is used, a literal IP address of the socket's family resolves to itself without calling `dns.lookup()","optional":true,"rest":false,"properties":[]},{"name":"signal","type":{"text":"AbortSignal","links":[{"name":"AbortSignal","href":"globals.html#class-abortsignal","start":0,"end":11}]},"description":"An AbortSignal that may be used to close a socket.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"receiveBlockList","type":{"text":"net.BlockList","links":[{"name":"net.BlockList","href":"net.html#class-netblocklist","start":0,"end":13}]},"description":"`receiveBlockList` can be used for discarding\ninbound datagram to specific IP addresses, IP ranges, or IP subnets. This does not\nwork if the server is behind a reverse proxy, NAT, etc. because the address\nchecked against the blocklist is the address of the proxy, or the one\nspecified by the NAT.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"sendBlockList","type":{"text":"net.BlockList","links":[{"name":"net.BlockList","href":"net.html#class-netblocklist","start":0,"end":13}]},"description":"`sendBlockList` can be used for disabling outbound\naccess to specific IP addresses, IP ranges, or IP subnets.","default":null,"optional":false,"rest":false,"properties":[]}]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Attached as a listener for `'message'` events. Optional.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":{"type":{"text":"dgram.Socket","links":[{"name":"dgram.Socket","href":"dgram.html#class-dgramsocket","start":0,"end":12}]},"description":""}},"description":"Creates a `dgram.Socket` object. Once the socket is created, calling\n[`socket.bind()`](#socketbindport-address-callback) will instruct the socket to begin listening for datagram\nmessages. When `address` and `port` are not passed to [`socket.bind()`](#socketbindport-address-callback) the\nmethod will bind the socket to the \"all interfaces\" address on a random port\n(it does the right thing for both `udp4` and `udp6` sockets). The bound address\nand port can be retrieved using [`socket.address().address`](#socketaddress) and\n[`socket.address().port`](#socketaddress).\n\nIf the `signal` option is enabled, calling `.abort()` on the corresponding\n`AbortController` is similar to calling `.close()` on the socket:\n\n```js\nconst 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```","summary":"Creates a `dgram.Socket` object. Once the socket is created, calling `socket.bind()` will instruct the socket to begin listening for datagram messages. When `address` and `port` are not passed to `socket.bind()` the method will bind the socket to the \"all interfaces\" address on a random port (it does the right thing for both `udp4` and `udp6` sockets). The bound address and port can be retrieved using `socket.address().address` and `socket.address().port`.","examples":[{"language":"js","displayName":null,"code":"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();"}],"children":[]},{"kind":"method","id":"dgramcreatesockettype-callback","name":"createSocket","title":"`dgram.createSocket(type[, callback])`","scope":"module","overloadOf":"dgramcreatesocketoptions-callback","stability":null,"added":["v0.1.99"],"deprecated":[],"removed":[],"napiVersion":[],"changes":[],"signature":{"parameters":[{"name":"type","type":{"text":"string","links":[{"name":"string","href":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type","start":0,"end":6}]},"description":"Either `'udp4'` or `'udp6'`.","default":null,"optional":false,"rest":false,"properties":[]},{"name":"callback","type":{"text":"Function","links":[{"name":"Function","href":"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function","start":0,"end":8}]},"description":"Attached as a listener to `'message'` events.","default":null,"optional":true,"rest":false,"properties":[]}],"returns":{"type":{"text":"dgram.Socket","links":[{"name":"dgram.Socket","href":"dgram.html#class-dgramsocket","start":0,"end":12}]},"description":""}},"description":"Creates a `dgram.Socket` object of the specified `type`.\n\nOnce the socket is created, calling [`socket.bind()`](#socketbindport-address-callback) will instruct the\nsocket to begin listening for datagram messages. When `address` and `port` are\nnot passed to [`socket.bind()`](#socketbindport-address-callback) the method will bind the socket to the \"all\ninterfaces\" address on a random port (it does the right thing for both `udp4`\nand `udp6` sockets). The bound address and port can be retrieved using\n[`socket.address().address`](#socketaddress) and [`socket.address().port`](#socketaddress).","summary":"Creates a `dgram.Socket` object of the specified `type`.","examples":[],"children":[]}]}]}