HSUnixDatagramSocket
TypeA Unix domain datagram socket, created by hs.socket.createUnixDatagram().
A Unix domain datagram socket exchanges messages with other programs on this Mac through a
socket file. Each message arrives whole, as one "data" event. Other programs can only send
to it once it has been bound to a path with bind(), which creates the socket file there.
This is what tools such as Karabiner-Elements' send_user_command send to.
- Note: macOS limits datagrams sent by other programs to 2048 bytes unless they raise their send buffer size (see
sysctl net.local.dgram.maxdgram). This socket raises its own, so it can send and receive up to 65536 bytes.
Do not instantiate HSUnixDatagramSocket directly - use hs.socket.createUnixDatagram().
Properties
Methods
setCallback(callback) -> HSUnixDatagramSocket
Sets the callback that receives this socket's events.
The callback receives the event name, the socket, and event-specific arguments:
"data"- a datagram arrived; the third argument is aUint8Arrayand the fourth is the sender's address, as{path}, ornullif the sender's socket isn't bound to a path (so it can't be replied to)."connected"-connect()succeeded; the fourth argument is the peer's address."error"- something went wrong; the third argument is a description.
setCallback(callback) -> HSUnixDatagramSocket
| Name | Type | Description |
|---|---|---|
| callback | function | null | The event handler, or null to remove it. |
sock.setCallback((event, sock, data, address) => {
if (event === "data") console.log("Received: " + hs.socket.decodeText(data))
})
bind(path) -> HSUnixDatagramSocket
Binds the socket to a path, creating a socket file there that other programs can send
datagrams to. Call it before connect() or sending.
A stale socket file left at the path by a socket that has gone is replaced, but binding
fails if another socket is still using it. close() removes the socket file.
bind(path) -> HSUnixDatagramSocket
| Name | Type | Description |
|---|---|---|
| path | string | The path to create the socket file at, at most 103 bytes long, e.g. "/tmp/hammerspoon.sock". |
// Receive Karabiner-Elements' send_user_command messages
globalThis.karabiner = hs.socket.createUnixDatagram().setCallback((event, sock, data) => {
if (event === "data") console.log(JSON.parse(hs.socket.decodeText(data)).key)
}).bind("/tmp/hammerspoon-karabiner.sock").receive()
connect(path) -> HSUnixDatagramSocket
Connects the socket to a single peer. Afterwards, send() needs no path, and only
datagrams from that peer are received. The callback receives "connected" straight away.
connect(path) -> HSUnixDatagramSocket
| Name | Type | Description |
|---|---|---|
| path | string | The path of the peer's socket file. |
hs.socket.createUnixDatagram().connect("/tmp/other.sock").send("hello")
send(text, path) -> HSUnixDatagramSocket
Sends text, encoded as UTF-8, as one datagram.
send(text, path) -> HSUnixDatagramSocket
| Name | Type | Description |
|---|---|---|
| text | string | The text to send. |
| path | string | The path of the socket file to send to. Omit it if the socket is connected. |
sock.send("hello", "/tmp/other.sock")
sendBytes(bytes, path) -> HSUnixDatagramSocket
Sends raw bytes as one datagram.
sendBytes(bytes, path) -> HSUnixDatagramSocket
| Name | Type | Description |
|---|---|---|
| bytes | JSValue | The bytes to send. Any typed array is accepted. |
| path | string | The path of the socket file to send to. Omit it if the socket is connected. |
sock.sendBytes(new Uint8Array([0xFF, 0x00]), "/tmp/other.sock")
receive() -> HSUnixDatagramSocket
Starts delivering every datagram that arrives to the callback, until pause() or
close(). The socket must be bound first (see bind()).
receive() -> HSUnixDatagramSocket
sock.bind("/tmp/hammerspoon.sock").receive()
receiveOne() -> HSUnixDatagramSocket
Delivers the next datagram that arrives to the callback, then stops receiving.
receiveOne() -> HSUnixDatagramSocket
sock.bind("/tmp/hammerspoon.sock").send("ping", "/tmp/other.sock").receiveOne()
pause() -> HSUnixDatagramSocket
Stops receiving datagrams. Datagrams that arrive meanwhile are queued by the system (up to its buffer size) and delivered once you receive again.
pause() -> HSUnixDatagramSocket
sock.pause()
close() -> HSUnixDatagramSocket
Closes the socket immediately, and removes its socket file if it's bound. The socket can be bound, connected or used to send again afterwards.
close() -> HSUnixDatagramSocket
sock.close()
destroy() -> None
Closes the socket and releases its callback.
destroy() -> None
sock.destroy()