Skip to content
Jennifer Programming Language

websocket API reference

An RFC 6455 WebSocket client over net. connect performs the HTTP Upgrade handshake (verifying the server's Sec-WebSocket-Accept, which is SHA-1 + base64 of the client key), then send / sendBytes write masked text / binary frames and receive reads the next message, transparently answering pings with pongs and reassembling fragmented messages. close sends a close frame and shuts the socket. ws:// is plain TCP, wss:// is TLS.

connectOptions takes an Options: extra handshake headers (an API key or a token), the subprotocols to offer (graphql-transport-ws), and TLS trust for a self-signed or private-CA server. The subprotocol the server chose is Conn.protocol.

Client-to-server frames are masked (required by the spec); server frames are not. Needs the default jennifer binary (net); a protocol error throws Error{kind: "websocket"}, a connection dropped mid-frame kind "closed". A server-side upgrade is out of scope (it needs an httpd connection-hijack hook).

Import with import "websocket.j" as websocket;. See the websocket guide for prose and examples.

Contents

NameWhat it is
closeSend a close frame and shut the connection.
connectOpen a WebSocket connection to a ws:// or wss:// URL (default receive timeout).
connectOptionsOpen a WebSocket connection with Options: extra handshake headers, the subprotocols to offer, TLS trust a...
connectWithOpen a WebSocket connection with an explicit per-receive read timeout.
defaultOptionsThe zero Options: what connect does.
pingSend a ping (empty payload).
receiveReceive the next message.
sendSend a text message.
sendBytesSend a binary message.
Connstruct: An open WebSocket connection.
Messagestruct: A received message.
Optionsstruct: How connectOptions opens a connection.

Functions

close

jennifer
websocket.close(c as Conn)

Send a close frame and shut the connection.

c
the connection

connect

jennifer
websocket.connect(url as string)

Open a WebSocket connection to a ws:// or wss:// URL (default receive timeout).

url
the ws:// or wss:// URL
Returns Conn
the open connection
Throws Error
kind "websocket" on a failed handshake

connectOptions

jennifer
websocket.connectOptions(url as string, options as Options)

Open a WebSocket connection with Options: extra handshake headers, the subprotocols to offer, TLS trust and the receive timeout. The subprotocol the server picked is protocol on the returned Conn ("" when it picked none; whether that is acceptable is the caller's call).

url
the ws:// or wss:// URL
options
how to connect
Returns Conn
the open connection
Throws Error
kind "websocket" on a bad URL or option, or a failed handshake

connectWith

jennifer
websocket.connectWith(url as string, timeoutMs as int)

Open a WebSocket connection with an explicit per-receive read timeout.

url
the ws:// or wss:// URL
timeoutMs
the per-receive read timeout in milliseconds
Returns Conn
the open connection
Throws Error
kind "websocket" on a failed handshake

defaultOptions

jennifer
websocket.defaultOptions()

The zero Options: what connect does. Set fields on the result.

Returns Options
the default options

ping

jennifer
websocket.ping(c as Conn)

Send a ping (empty payload).

c
the connection

receive

jennifer
websocket.receive(c as Conn)

Receive the next message. Pings are answered with pongs and skipped; fragmented text / binary messages are reassembled. A close frame is returned as kind "close".

c
the connection
Returns Message
the next message
Throws Error
kind "websocket" on a protocol error, "closed" for a connection dropped mid-frame

send

jennifer
websocket.send(c as Conn, text as string)

Send a text message.

c
the connection
text
the message text

sendBytes

jennifer
websocket.sendBytes(c as Conn, data as bytes)

Send a binary message.

c
the connection
data
the message payload

Structs

Conn

An open WebSocket connection.

jennifer
def struct Conn {
    socket as net.Conn,
    timeoutMs as int,
    protocol as string
};
socket
the underlying (plain or TLS) connection
timeoutMs
the per-receive read timeout in milliseconds
protocol
the subprotocol the server chose from those offered, "" for none

Message

A received message.

jennifer
def struct Message {
    kind as string,
    text as string,
    data as bytes
};
kind
"text", "binary", "close", or "pong"
text
the decoded text (for a "text" message; "" otherwise)
data
the raw payload bytes

Options

How connectOptions opens a connection. The zero value (from websocket.defaultOptions()) is what connect does: the 30 s receive timeout, no extra headers, no subprotocol, and TLS verified against the system roots.

jennifer
def struct Options {
    timeoutMs as int,
    headers as map of string to string,
    protocols as list of string,
    caCert as bytes,
    skipVerify as bool
};
timeoutMs
the per-receive read timeout in milliseconds (0 = 30 s)
headers
extra headers for the handshake request, such as an API key
protocols
the subprotocols to offer, in order of preference (Sec-WebSocket-Protocol)
caCert
a PEM certificate to trust besides the system roots, for a private CA or a self-signed wss:// server
skipVerify
accept any certificate; for a trusted LAN endpoint only, it exposes the connection to a man-in-the-middle