UDP / Datagram Sockets
The module provides an implementation of UDP Datagram sockets.
The object is an that encapsulates the datagram functionality.
New instances of are created using
The keyword is not to be used to create instances.
The event is emitted after a socket is closed with
Once triggered, no new events will be emitted on this socket.
The event is emitted whenever any error occurs.
The event handler function is passed a single object.
The event is emitted whenever a socket begins listening for datagram messages.
This occurs as soon as UDP sockets are created.
The event is emitted when a new datagram is available on a socket.
The event handler function is passed two arguments: and
Remote address information.
The sender address.
The address family
The sender port.
The message size.
Tells the kernel to join a multicast group at the given and using the socket option.
If the 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 multiple times, once per interface.
Returns an object containing the address information for a socket.
For UDP sockets, this object will contain and properties.
with no parameters.
Called when binding is complete.
For UDP sockets, causes the to listen for datagram messages on a named and optional
If is not specified or is, the operating system will attempt to bind to a random port.
If is not specified, the operating system will attempt to listen on all addresses.
Once binding is complete, a event is emitted and the optional function is called.
Note that specifying both a event listener and passing a to the method is not harmful but not very useful.
A bound datagram socket keeps the Node.js process running to receive datagram messages.
If binding fails, an event is generated.
In rare case (e.g. attempting to bind with a closed socket), an may be thrown.
Example of a UDP server listening on port 41234:
Supports the following properties:
The object may contain an additional property that is use when using objects with the module.
When is set to (the default), cluster workers will use the same underlying socket handle allowing connection handling duties to be shared.
When is, however, the handle is not shared and attempted port sharing results in an error
An example socket listening on an exclusive port is shown below.
Close the underlying socket and stop listening for data on it.
If a callback is provided, it is added as a listener for the event.
Instructs the kernel to leave a multicast group at using the 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.
If is not specified, the operating system will attempt to drop membership on all valid interfaces.
Returns: the socket receive buffer size in bytes.
By default, binding a socket will cause it to block the Node.js process from exiting as long as the socket is open.
The method can be used to exclude the socket from the reference counting that keeps the Node.js process active.
The method adds the socket back to the reference counting and restores the default behavior.
Calling multiples times will have no additional effect.
The method returns a reference to the socket so calls can be chained.
Message to be sent.
Offset in the buffer where the message starts.
Number of bytes in the message.
Destination hostname or IP address.
Called when the message has been sent.
Broadcasts a datagram on the socket.
The destination and must be specified.
The argument contains the message to be sent.
Depending on its type, different behavior can apply.
If is a or the and specify the offset within the where the message begins and the number of bytes in the message, respectively.
If is a, then it is automatically converted to a with encoding.
With messages that contain multi-byte characters, and will be calculated with respect to and not the character position.
If is an array, and must not be specified.
The argument is a string.
If the value of is a host name, DNS will be used to resolve the address of the host.
If is not provided or otherwise falsy, (for sockets) or will be used by default.
If the socket has not been previously bound with a call to, the socket is assigned a random port number and is bound to the "all interfaces" address
An optional function may be specified to as a way of reporting DNS errors or for determining when it is safe to reuse the object.
Note that DNS lookups delay the time to send for at least one tick of the Node.js event loop.
The only way to know for sure that the datagram has been sent is by using a
If an error occurs and a is given, the error will be passed as the first argument to the
If a is not given, the error is emitted as an event on the object.
Offset and length are optional but both must be set if either are used.
They are supported only when the first argument is a or
Sending multiple buffers might be faster or slower depending on the application and operating system.
It is important to run benchmarks to determine the optimal strategy on a case-by-case basis.
Generally speaking, however, sending multiple buffers is faster.
A Note about UDP datagram size
The maximum size of an datagram depends on the (Maximum Transmission Unit) and on the field size.
The field is wide, which means that a normal payload exceed 64K octets including the internet header and data this is generally true for loopback interfaces, but such long datagram messages are impractical for most hosts and networks.
The is the largest size a given link layer technology can support for datagram messages.
For any link, mandates a minimum of octets, while the recommended for IPv4 is (typically recommended as the for dial-up type applications), whether they arrive whole or in fragments.
For, the minimum is octets, however, the mandatory minimum fragment reassembly buffer size is octets.
The value of octets is very small, since most current link layer technologies, like Ethernet, have a minimum of
It is impossible to know in advance the MTU of each link through which a packet might travel. Sending a datagram greater than the receiver MTU will not work because the packet will get silently dropped without informing the source that the data did not reach its intended recipient.