hs.socket
ModuleModule for communicating over raw TCP, UDP and Unix domain sockets.
hs.socket.create() makes a stream socket, which can connect to a TCP server or Unix domain
socket (optionally over TLS), or listen for incoming connections. hs.socket.createUDP()
makes a UDP socket, which can send and receive datagrams, including broadcasts.
hs.socket.createUnixDatagram() makes a Unix domain datagram socket, which exchanges
datagrams with other programs on this Mac through a socket file.
Sockets report everything through a single callback, set with setCallback(). Received data
is delivered as a Uint8Array; hs.socket.decodeText() turns it into a string.
TCP client
hs.socket.create().setCallback((event, sock, data) => {
if (event === "connected") sock.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n").readUntil("\r\n\r\n")
if (event === "data") console.log(hs.socket.decodeText(data))
if (event === "error") console.error(data)
}).connect("example.com", 80)
TCP server
Each connection a listening socket accepts arrives as a new socket, with the same callback.
const server = hs.socket.create().setCallback((event, sock, data) => {
if (event === "connection") data.readUntil("\n")
if (event === "data") sock.write("You said: " + hs.socket.decodeText(data)).readUntil("\n")
}).listen(9000)
UDP
const udp = hs.socket.createUDP().setCallback((event, sock, data, address) => {
if (event === "data") console.log(address.host + ": " + hs.socket.decodeText(data))
}).bind(9999).receive()
udp.send("hello", "127.0.0.1", 9999)
Unix domain datagrams
For example, to receive messages from Karabiner-Elements' send_user_command:
globalThis.karabiner = hs.socket.createUnixDatagram().setCallback((event, sock, data) => {
if (event === "data") console.log(JSON.parse(hs.socket.decodeText(data)))
}).bind("/tmp/hammerspoon-karabiner.sock").receive()
Types
This module provides the following types:
Properties
hs.socket.connectTimeout
The connect timeout, in seconds, that new stream sockets start with (see
HSSocket.connectTimeout). 0 means no timeout, which is the default.
hs.socket.readTimeout
The read timeout, in seconds, that new stream sockets start with (see
HSSocket.readTimeout). 0 means no timeout, which is the default.
Methods
hs.socket.create() -> HSSocket
Creates a TCP or Unix domain stream socket. It does nothing until you connect it or make it listen.
hs.socket.create() -> HSSocket
HSSocket.const sock = hs.socket.create().setCallback(handler).connect("localhost", 6379)
hs.socket.createUDP() -> HSUDPSocket
Creates a UDP socket.
hs.socket.createUDP() -> HSUDPSocket
HSUDPSocket.const udp = hs.socket.createUDP().setCallback(handler).bind(9999).receive()
hs.socket.createUnixDatagram() -> HSUnixDatagramSocket
Creates a Unix domain datagram socket.
hs.socket.createUnixDatagram() -> HSUnixDatagramSocket
HSUnixDatagramSocket.const sock = hs.socket.createUnixDatagram().setCallback(handler).bind("/tmp/hammerspoon.sock").receive()
hs.socket.decodeText(bytes, lossy) -> string
Decodes bytes as UTF-8 text.
Data from readBytes() or readAvailable() can end part-way through a multi-byte
character, whose remaining bytes arrive in the next read. Such bytes aren't valid UTF-8 on
their own, so join the reads' bytes before decoding them, or use readUntil() with a
delimiter, whose data always ends with a complete character.
hs.socket.decodeText(bytes, lossy) -> string
| Name | Type | Description |
|---|---|---|
| bytes | JSValue | The bytes to decode, e.g. the data from a "data" event. |
| lossy | boolean | true to replace invalid or incomplete UTF-8 with the replacement character (U+FFFD) instead of returning null. Defaults to false. |
null if the bytes aren't valid UTF-8 (including when they end part-way through a character) and lossy isn't true.console.log(hs.socket.decodeText(new Uint8Array([104, 105]))) // "hi"
console.log(hs.socket.decodeText(new Uint8Array([104, 0xE2]), true)) // "h\uFFFD"