module dgram

Supported only in the v8 engine. Introduced in Max v9.2.0.

The dgram module exposed by require("dgram").

IPv6 parity is limited and UDP socket-option coverage is narrow.

Example

const dgram = require("dgram");
const socket = dgram.createSocket("udp4");
socket.on("message", (msg, rinfo) =>
  post(`udp ${rinfo.address}:${rinfo.port} ${msg.toString()}`, "\n"),
);
socket.bind(41234, "127.0.0.1", () => socket.send("ping", 41234, "127.0.0.1"));

Index

Functions

Classes

Interfaces

function createSocket

Creates a UDP dgram.Socket.

function createSocket(type: "udp4" | "udp6"): Socket;
NameTypeDescription
type"udp4" | "udp6"
Return ValueSocket

interface RemoteInfo

The sender information passed with a UDP message event.

Properties

address string

port number

class Socket

A UDP socket.

Incoming message payloads arrive as buffer.IOBuffer. Socket-option setters throw a structured error (syscall: "setsockopt") on failure and lazily open the socket if it has not been bound. Events: message, listening, close, error.

Properties

Methods

addMembership(multicastAddress, interfaceAddress)

Joins a multicast group.

addMembership(multicastAddress: string, interfaceAddress?: string): void;
NameTypeDescription
multicastAddressstring
optional interfaceAddressstring

address()

Returns the bound local endpoint.

address(): AddressInfo;
NameTypeDescription
Return ValueAddressInfo

bind(port, address, callback)

Binds the socket to a local port/address.

bind(port: number, address?: string, callback?: () => void): void;
NameTypeDescription
portnumber
optional addressstring
optional callback() => void

close()

Closes the socket.

close(): void;

dropMembership(multicastAddress, interfaceAddress)

Leaves a multicast group.

dropMembership(multicastAddress: string, interfaceAddress?: string): void;
NameTypeDescription
multicastAddressstring
optional interfaceAddressstring

off(event, listener)

Removes an event listener.

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

Registers an event listener.

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

Registers a one-shot event listener.

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

send(msg, port, address, callback)

Sends a datagram.

send(
  msg: string | buffer.IOBuffer | ArrayBufferView,
  port: number,
  address?: string,
  callback?: (error?: Error) => void,
): void;
NameTypeDescription
msgstring | buffer.IOBuffer | ArrayBufferView
portnumber
optional addressstring
optional callback(error?: Error) => void

setBroadcast(flag)

Enables or disables broadcast.

setBroadcast(flag: boolean): void;
NameTypeDescription
flagboolean

setMulticastInterface(iface)

Selects the outgoing multicast interface (IPv4 dotted-quad only).

setMulticastInterface(iface: string): void;
NameTypeDescription
ifacestring

setMulticastLoopback(flag)

Enables or disables multicast loopback.

setMulticastLoopback(flag: boolean): void;
NameTypeDescription
flagboolean

setMulticastTTL(ttl)

Sets the multicast time-to-live.

setMulticastTTL(ttl: number): void;
NameTypeDescription
ttlnumber

setTTL(ttl)

Sets the unicast time-to-live.

setTTL(ttl: number): void;
NameTypeDescription
ttlnumber

type string read-only

The socket protocol, "udp4" or "udp6".