API Docs

A UDP socket, created by hs.socket.createUDP().

A UDP socket can send datagrams to any host, or be connected to a single peer, and can receive datagrams once it has a local port: either one chosen with bind(), or one the system assigns when it first connects or sends.

IPv4 and IPv6 are both supported, using a separate system socket for each.

Do not instantiate HSUDPSocket directly - use hs.socket.createUDP().

Properties

identifier

string
A unique identifier for this socket (UUID string).

isBound

boolean
Whether the socket has been bound to a local port with `bind()`.

isConnected

boolean
Whether the socket is connected to a single peer with `connect()`.

isReceiving

boolean
Whether the socket is currently receiving datagrams.

localPort

number
The local port the socket is using, or 0 if it doesn't have one yet.

broadcast

boolean
Whether the socket may send to broadcast addresses (e.g. `255.255.255.255`). Defaults to `false`. Broadcasts are IPv4 only.

reusePort

boolean
Whether other sockets may bind to the same port, e.g. so several programs can receive the same broadcasts. Defaults to `false`. Set it before calling `bind()`.

ipv4Enabled

boolean
Whether the socket uses IPv4. Defaults to `true`. Set it before the socket binds, connects or sends.

ipv6Enabled

boolean
Whether the socket uses IPv6. Defaults to `true`. Set it before the socket binds, connects or sends.

Methods

setCallback(callback) -> HSUDPSocket

Sets the callback that receives this socket's events. sender's address, as `{host, port, family}`, where family is `"ipv4"` or `"ipv6"`.
setCallback(callback) -> HSUDPSocket
Name Type Description
callback function | null The event handler, or `null` to remove it.
HSUDPSocket
The socket, for chaining.
udp.setCallback((event, sock, data, address) => {
    if (event === "data") console.log(address.host + " sent " + hs.socket.decodeText(data))
})

bind(port, address) -> HSUDPSocket

Binds the socket to a local port, so it can receive datagrams sent to that port. Call it before `connect()` or sending. By default the socket binds on all network interfaces. Pass one of this Mac's IP addresses to bind only to that address (and so only to the interface that has it). Use `hs.network.addresses()` to find an interface's addresses. If the interface's address can change (e.g. it comes from DHCP), watch `State:/Network/Interface/<name>/IPv4` with `hs.network.configurationWatcher()` and bind again when it does.
bind(port, address) -> HSUDPSocket
Name Type Description
port number The UDP port to bind to, or 0 to let the system choose one (see `localPort`).
address string A local IPv4 or IPv6 address to bind to, e.g. `"127.0.0.1"` or `"192.168.1.20"`. Defaults to all addresses.
HSUDPSocket
The socket, for chaining.
A socket bound to an address only uses that address's family (IPv4 or IPv6), and doesn't receive broadcasts
hs.socket.createUDP().setCallback(handler).bind(9999).receive()

// Only on the Wi-Fi interface's IPv4 address
const wifi = hs.network.addresses().find(a => a.interface === "en0" && a.family === "ipv4")
hs.socket.createUDP().setCallback(handler).bind(9999, wifi.address).receive()

connect(host, port) -> HSUDPSocket

Connects the socket to a single peer. Afterwards, `send()` needs no host and port, and only datagrams from that peer are received. The callback receives `"connected"` once the host has been looked up.
connect(host, port) -> HSUDPSocket
Name Type Description
host string The host name or IP address of the peer.
port number The peer's UDP port.
HSUDPSocket
The socket, for chaining.
hs.socket.createUDP().connect("192.168.1.10", 9999).send("hello").receive()

send(text, host, port) -> HSUDPSocket

Sends text, encoded as UTF-8, as one datagram.
send(text, host, port) -> HSUDPSocket
Name Type Description
text string The text to send.
host string The host name or IP address to send to. Omit it if the socket is connected.
port number The UDP port to send to. Omit it if the socket is connected.
HSUDPSocket
The socket, for chaining.
udp.send("hello", "127.0.0.1", 9999)

sendBytes(bytes, host, port) -> HSUDPSocket

Sends raw bytes as one datagram.
sendBytes(bytes, host, port) -> HSUDPSocket
Name Type Description
bytes JSValue The bytes to send. Any typed array is accepted.
host string The host name or IP address to send to. Omit it if the socket is connected.
port number The UDP port to send to. Omit it if the socket is connected.
HSUDPSocket
The socket, for chaining.
udp.sendBytes(new Uint8Array([0xFF, 0x00]), "127.0.0.1", 9999)

receive() -> HSUDPSocket

Starts delivering every datagram that arrives to the callback, until `pause()` or `close()`. The socket must have a local port first (see `bind()`).
receive() -> HSUDPSocket
HSUDPSocket
The socket, for chaining.
A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it
udp.bind(9999).receive()

receiveOne() -> HSUDPSocket

Delivers the next datagram that arrives to the callback, then stops receiving.
receiveOne() -> HSUDPSocket
HSUDPSocket
The socket, for chaining.
A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it
udp.send("ping", "127.0.0.1", 9999).receiveOne()

pause() -> HSUDPSocket

Stops receiving datagrams. Datagrams that arrive meanwhile are queued by the system (up to its buffer size) and delivered once you receive again.
pause() -> HSUDPSocket
HSUDPSocket
The socket, for chaining.
udp.pause()

close() -> HSUDPSocket

Closes the socket immediately, discarding any sends still waiting for a host lookup. The socket can be bound, connected or used to send again afterwards.
close() -> HSUDPSocket
HSUDPSocket
The socket, for chaining.
udp.close()

destroy() -> None

Closes the socket and releases its callback.
destroy() -> None
None
udp.destroy()