WebSocket

represents an active WebSocket connection.

Handlers receive a reference to this and call WebSocket.send or WebSocket.send-binary to queue outgoing text or binary frames. The event loop drains the outbox after the handler returns.

The protocol field holds the negotiated subprotocol (RFC 6455 ยง4.2.2), or Nothing if no subprotocol was negotiated.

copy

instantiate

(Fn [(Ref WebSocket a)] WebSocket)

copies a WebSocket.

delete

instantiate

(Fn [WebSocket] ())

deletes a WebSocket. Should usually not be called manually.

encode-binary

defn

(Fn [(Ref (Array Byte) a)] (Array Byte))

                        (encode-binary data)
                    

encodes a byte array as a WebSocket binary frame (no mask).

encode-ping

defn

(Fn [(Ref (Array Byte) a)] (Array Byte))

                        (encode-ping payload)
                    

encodes a WebSocket ping frame with the given payload (may be empty).

encode-text

defn

(Fn [(Ref String a)] (Array Byte))

                        (encode-text msg)
                    

encodes a string as a WebSocket text frame (no mask).

fd

instantiate

(Fn [(Ref WebSocket a)] (Ref Int a))

gets the fd property of a WebSocket.

init

instantiate

(Fn [Int, (Maybe String), (Array (Array Byte))] WebSocket)

creates a WebSocket.

outbox

instantiate

(Fn [(Ref WebSocket a)] (Ref (Array (Array Byte)) a))

gets the outbox property of a WebSocket.

prn

instantiate

(Fn [(Ref WebSocket a)] String)

converts a WebSocket to a string.

protocol

instantiate

(Fn [(Ref WebSocket a)] (Ref (Maybe String) a))

gets the protocol property of a WebSocket.

send

defn

(Fn [(Ref WebSocket a), (Ref String b)] ())

                        (send ws msg)
                    

queues a text message for sending on the WebSocket connection.

send-binary

defn

(Fn [(Ref WebSocket a), (Ref (Array Byte) b)] ())

                        (send-binary ws data)
                    

queues a binary message for sending on the WebSocket connection.

send-binary-now

defn

(Fn [(Ref WebSocket a), (Ref (Array Byte) b)] Bool)

                        (send-binary-now ws data)
                    

sends a binary message immediately, bypassing the outbox. Like send-now but for binary data.

send-now

defn

(Fn [(Ref WebSocket a), (Ref String b)] Bool)

                        (send-now ws msg)
                    

sends a text message immediately, bypassing the outbox. Writes the WebSocket frame directly to the socket, waiting for the socket to drain if the kernel send buffer is full. Use this inside handlers that block for a long time (e.g. LLM streaming) so that each message reaches the client without waiting for the handler to return.

Returns true when the whole frame was written. Returns false when the connection is dead or the client stopped draining; the caller should stop sending on this connection.

set-fd

instantiate

(Fn [WebSocket, Int] WebSocket)

sets the fd property of a WebSocket.

set-fd!

instantiate

(Fn [(Ref WebSocket a), Int] ())

sets the fd property of a WebSocket in place.

set-outbox

instantiate

(Fn [WebSocket, (Array (Array Byte))] WebSocket)

sets the outbox property of a WebSocket.

set-outbox!

instantiate

(Fn [(Ref WebSocket a), (Array (Array Byte))] ())

sets the outbox property of a WebSocket in place.

set-protocol

instantiate

(Fn [WebSocket, (Maybe String)] WebSocket)

sets the protocol property of a WebSocket.

set-protocol!

instantiate

(Fn [(Ref WebSocket a), (Maybe String)] ())

sets the protocol property of a WebSocket in place.

str

instantiate

(Fn [(Ref WebSocket a)] String)

converts a WebSocket to a string.

update-fd

instantiate

(Fn [WebSocket, (Ref (Fn [Int] Int a) b)] WebSocket)

updates the fd property of a WebSocket using a function f.

update-outbox

instantiate

(Fn [WebSocket, (Ref (Fn [(Array (Array Byte))] (Array (Array Byte)) a) b)] WebSocket)

updates the outbox property of a WebSocket using a function f.

update-protocol

instantiate

(Fn [WebSocket, (Ref (Fn [(Maybe String)] (Maybe String) a) b)] WebSocket)

updates the protocol property of a WebSocket using a function f.