Skip to content

DgramSocket

Defined in: packages/node/src/net/dgram-socket.ts:77

High-level wrapper around Node.js dgram.Socket | UDP datagram sockets.

This class delegates almost all behavior to an underlying dgram.Socket instance while:

Unless explicitly documented otherwise, methods have the same semantics as their counterparts on dgram.Socket.

Example usage:

const dgramSocket = DgramSocket.from({ type: "udp4" });
await dgramSocket.bind(12345, "localhost");
dgramSocket.on("message", (msg, msgSize, from) => {
console.log(
`Received ${msgSize} bytes from ${from.address}:${from.port}`,
);
});
await dgramSocket.send(54321, "localhost", Buffer.from("Hello, UDP!"));
await dgramSocket.close();

new DgramSocket(socket): DgramSocket

Defined in: packages/node/src/net/dgram-socket.ts:92

Constructs a new DgramSocket instance wrapping the provided dgram.Socket.

Socket

DgramSocket

EventDispatcherMapBase<DgramSocketEvents>.constructor

addMembership(multicastAddress, multicastInterface?): void

Defined in: packages/node/src/net/dgram-socket.ts:132

Adds this socket to the given multicast group.

This is a thin wrapper around dgram.Socket.addMembership, which configures IP_ADD_MEMBERSHIP for the given multicast group.

string

string

void


address(): InetEndpoint | null

Defined in: packages/node/src/net/dgram-socket.ts:272

Gets the address information of the bound socket.

InetEndpoint | null

The socket’s endpoint information or null if the socket is not bound.

If the address type is not an AddressInfo object.


addSourceMembership(sourceAddress, groupAddress, multicastInterface?): void

Defined in: packages/node/src/net/dgram-socket.ts:193

Adds this socket to a source-specific multicast group.

This is a thin wrapper around dgram.Socket.addSourceSpecificMembership, configuring source-specific multicast membership for the given source and group.

string

string

string

void


bind(port?, address?, options?): Promise<void>

Defined in: packages/node/src/net/dgram-socket.ts:304

Binds the UDP socket to the specified address and port.

number = 0

The port to bind to. If not specified, a random available port will be used.

string

The address to bind to. If not specified, the socket will bind to all available interfaces.

Binding options.

boolean

number

Promise<void>

A promise that resolves when the socket is successfully bound.


close(): Promise<void>

Defined in: packages/node/src/net/dgram-socket.ts:416

Closes the UDP socket.

Promise<void>

A promise that resolves when the socket is successfully closed.


connect(port, address?): Promise<void>

Defined in: packages/node/src/net/dgram-socket.ts:350

Associates the socket with one remote endpoint, binding it to a random port first if it is not bound.

Once connected, datagrams from any other endpoint are dropped by the kernel, DgramSocket.sendConnected sends to the remote endpoint, and an ICMP port unreachable it answers with surfaces as an ECONNREFUSED error event rather than being ignored.

number

The remote port.

string

The remote address; resolved like send’s, and defaulting to the loopback address.

Promise<void>

A promise that resolves once the socket is connected, and rejects when it already is or when binding or resolving address fails.


disconnect(): void

Defined in: packages/node/src/net/dgram-socket.ts:384

Dissociates a connected socket from its remote endpoint.

This is a thin wrapper around dgram.Socket.disconnect.

void

With code ERR_SOCKET_DGRAM_NOT_CONNECTED when the socket is not connected.


dropMembership(multicastAddress, multicastInterface?): void

Defined in: packages/node/src/net/dgram-socket.ts:182

Removes this socket from the given multicast group.

This is a thin wrapper around dgram.Socket.dropMembership, which configures IP_DROP_MEMBERSHIP for the given multicast group.

string

string

void


dropSourceMembership(sourceAddress, groupAddress, multicastInterface?): void

Defined in: packages/node/src/net/dgram-socket.ts:212

Removes this socket from a source-specific multicast group.

This is a thin wrapper around dgram.Socket.dropSourceSpecificMembership, removing source-specific multicast membership for the given source and group.

string

string

string

void


getRecvBufferSize(): number

Defined in: packages/node/src/net/dgram-socket.ts:152

Returns the size in bytes of the underlying receive buffer.

This is a thin wrapper around dgram.Socket.getRecvBufferSize, which reads the SO_RCVBUF socket option.

number


getSendBufferSize(): number

Defined in: packages/node/src/net/dgram-socket.ts:172

Returns the size in bytes of the underlying send buffer.

This is a thin wrapper around dgram.Socket.getSendBufferSize, which reads the SO_SNDBUF socket option.

number


getStat(): DgramSocketStat

Defined in: packages/node/src/net/dgram-socket.ts:437

Returns statistics about the underlying send queue.

This wraps dgram.Socket.getSendQueueCount and dgram.Socket.getSendQueueSize into a single object.

DgramSocketStat


isSubscribed<K>(event, listener): boolean

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:38

Check if a callback is subscribed to a specific event.

K extends keyof DgramSocketEvents

K

The event name.

Callable<DgramSocketEvents[K]>

The callback to check.

boolean

IEventDispatcherMap.isSubscribed

EventDispatcherMapBase.isSubscribed


ref(): void

Defined in: packages/node/src/net/dgram-socket.ts:101

References the socket, preventing the process from exiting while the socket is active.

void


remoteAddress(): InetEndpoint | null

Defined in: packages/node/src/net/dgram-socket.ts:393

Gets the remote endpoint of a connected socket.

InetEndpoint | null

The remote endpoint, or null if the socket is not connected.


send(port, address, msg, offset?, length?): Promise<number>

Defined in: packages/node/src/net/dgram-socket.ts:467

Sends a message to the specified address and port.

The msg argument contains the message to be sent. Depending on its type, different behavior can apply. If msg is a Buffer, any TypedArray or a DataView, the offset and length specify the offset within the Buffer where the message begins and the number of bytes in the message, respectively.

If msg is a String, then it is automatically converted to a Buffer with ‘utf8’ encoding.

With messages that contain multi-byte characters, offset and length will be calculated with respect to byte length and not the character position. If msg is an array, offset and length must not be specified.

number

The destination port.

string

The destination address.

string | DataView<ArrayBufferLike> | TypedArray

The message to send. This can be a Buffer, TypedArray, or DataView.

number

Optional offset in the message buffer to start sending from.

number

Optional number of bytes to send from the message buffer.

Promise<number>

A promise that resolves with the number of bytes sent.


sendConnected(msg, offset?, length?): Promise<number>

Defined in: packages/node/src/net/dgram-socket.ts:503

Sends a message to the remote endpoint of a connected socket.

The message, offset and length behave as in DgramSocket.send.

string | DataView<ArrayBufferLike> | TypedArray

The message to send.

number

Optional offset in the message buffer to start sending from.

number

Optional number of bytes to send from the message buffer.

Promise<number>

A promise that resolves with the number of bytes sent, and rejects when the socket is not connected.


setBroadcast(flag): void

Defined in: packages/node/src/net/dgram-socket.ts:122

Enables or disables sending of broadcast datagrams.

This is a thin wrapper around dgram.Socket.setBroadcast, which toggles the SO_BROADCAST socket option. When enabled, UDP packets may be sent to broadcast addresses.

boolean

Whether broadcast should be enabled.

void


setMulticastInterface(multicastInterface): void

Defined in: packages/node/src/net/dgram-socket.ts:230

Sets the outbound multicast interface.

This is a thin wrapper around dgram.Socket.setMulticastInterface, which controls the default outgoing interface for multicast traffic.

string

void


setMulticastLoop(flag): void

Defined in: packages/node/src/net/dgram-socket.ts:240

Enables or disables loopback for multicast packets sent from this socket.

This is a thin wrapper around dgram.Socket.setMulticastLoopback, which toggles the IP_MULTICAST_LOOP socket option.

boolean

void


setMulticastTtl(ttl): void

Defined in: packages/node/src/net/dgram-socket.ts:250

Sets the time-to-live (TTL) value for multicast packets.

This is a thin wrapper around dgram.Socket.setMulticastTTL, which configures the IP_MULTICAST_TTL socket option.

number

void


setRecvBufferSize(size): void

Defined in: packages/node/src/net/dgram-socket.ts:142

Sets the size in bytes of the underlying receive buffer.

This is a thin wrapper around dgram.Socket.setRecvBufferSize, which sets the SO_RCVBUF socket option.

number

void


setSendBufferSize(size): void

Defined in: packages/node/src/net/dgram-socket.ts:162

Sets the size in bytes of the underlying send buffer.

This is a thin wrapper around dgram.Socket.setSendBufferSize, which sets the SO_SNDBUF socket option.

number

void


setTtl(ttl): void

Defined in: packages/node/src/net/dgram-socket.ts:260

Sets the unicast time-to-live (TTL) value for outgoing packets.

This is a thin wrapper around dgram.Socket.setTTL, which configures the IP_TTL socket option.

number

void


subscribe<K>(event, listener, options?): Callable | null

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:23

K extends keyof DgramSocketEvents

K

Callable<DgramSocketEvents[K]>

EventDispatcherSubscribeOptions

Callable | null

IEventDispatcherMap.subscribe

EventDispatcherMapBase.subscribe


unref(): void

Defined in: packages/node/src/net/dgram-socket.ts:109

Unreferences the socket, allowing the process to exit even if the socket is active.

void


unsubscribe<K>(event, listener): void

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:31

Unsubscribe a callback from a specific event.

K extends keyof DgramSocketEvents

K

The event name.

Callable<DgramSocketEvents[K]>

The callback to remove.

void

IEventDispatcherMap.unsubscribe

EventDispatcherMapBase.unsubscribe


wait<K>(event, options?): Promise<DgramSocketEvents[K]>

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:45

Wait for the next occurrence of a specific event, optionally matching a predicate.

This is a convenience method that subscribes to the dispatcher, waits for the next event that matches the predicate (if provided), and then unsubscribes automatically.

K extends keyof DgramSocketEvents

K

The event name to wait for.

EventDispatcherWaitOptions<DgramSocketEvents[K]>

Optional settings including predicate and abort signal.

Promise<DgramSocketEvents[K]>

A promise that resolves with the event arguments as an array.

IEventDispatcherMap.wait

EventDispatcherMapBase.wait


static from(options?): DgramSocket

Defined in: packages/node/src/net/dgram-socket.ts:82

Creates a new DgramSocket instance with the specified options.

SocketOptions

DgramSocket