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;
| Name | Type | Description |
|---|---|---|
| type | "udp4" | "udp6" | |
| Return Value | Socket |
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)
- address()
- bind(port, address, callback)
- close()
- dropMembership(multicastAddress, interfaceAddress)
- off(event, listener)
- on(event, listener)
- once(event, listener)
- send(msg, port, address, callback)
- setBroadcast(flag)
- setMulticastInterface(iface)
- setMulticastLoopback(flag)
- setMulticastTTL(ttl)
- setTTL(ttl)
addMembership(multicastAddress, interfaceAddress)
Joins a multicast group.
addMembership(multicastAddress: string, interfaceAddress?: string): void;
| Name | Type | Description |
|---|---|---|
| multicastAddress | string | |
| optional interfaceAddress | string |
address()
Returns the bound local endpoint.
address(): AddressInfo;
| Name | Type | Description |
|---|---|---|
| Return Value | AddressInfo |
bind(port, address, callback)
Binds the socket to a local port/address.
bind(port: number, address?: string, callback?: () => void): void;
| Name | Type | Description |
|---|---|---|
| port | number | |
| optional address | string | |
| optional callback | () => void |
close()
Closes the socket.
close(): void;
dropMembership(multicastAddress, interfaceAddress)
Leaves a multicast group.
dropMembership(multicastAddress: string, interfaceAddress?: string): void;
| Name | Type | Description |
|---|---|---|
| multicastAddress | string | |
| optional interfaceAddress | string |
off(event, listener)
Removes an event listener.
off(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
Registers an event listener.
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
Registers a one-shot event listener.
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
send(msg, port, address, callback)
Sends a datagram.
send(
msg: string | buffer.IOBuffer | ArrayBufferView,
port: number,
address?: string,
callback?: (error?: Error) => void,
): void;
| Name | Type | Description |
|---|---|---|
| msg | string | buffer.IOBuffer | ArrayBufferView | |
| port | number | |
| optional address | string | |
| optional callback | (error?: Error) => void |
setBroadcast(flag)
Enables or disables broadcast.
setBroadcast(flag: boolean): void;
| Name | Type | Description |
|---|---|---|
| flag | boolean |
setMulticastInterface(iface)
Selects the outgoing multicast interface (IPv4 dotted-quad only).
setMulticastInterface(iface: string): void;
| Name | Type | Description |
|---|---|---|
| iface | string |
setMulticastLoopback(flag)
Enables or disables multicast loopback.
setMulticastLoopback(flag: boolean): void;
| Name | Type | Description |
|---|---|---|
| flag | boolean |
setMulticastTTL(ttl)
Sets the multicast time-to-live.
setMulticastTTL(ttl: number): void;
| Name | Type | Description |
|---|---|---|
| ttl | number |
setTTL(ttl)
Sets the unicast time-to-live.
setTTL(ttl: number): void;
| Name | Type | Description |
|---|---|---|
| ttl | number |
type string read-only
The socket protocol, "udp4" or "udp6".