API Docs

Module 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.

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)

Types

This module provides the following types:

Properties

hs.socket.connectTimeout

number
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

number
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
A new `HSSocket`.
const sock = hs.socket.create().setCallback(handler).connect("localhost", 6379)

hs.socket.createUDP() -> HSUDPSocket

Creates a UDP socket.
hs.socket.createUDP() -> HSUDPSocket
HSUDPSocket
A new `HSUDPSocket`.
const udp = hs.socket.createUDP().setCallback(handler).bind(9999).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`.
string
The text, or `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"