Module

whiplash/websocket/route

src/whiplash/websocket/route.pooka

WebSocket routes: what a server does with a conversation once a client has upgraded, over HTTP/1.1, HTTP/2 (RFC 8441) or HTTP/3 (RFC 9220).

A Route answers each message with the messages to send back. PONGs and close replies are automatic. with_open adds a function that runs beside the conversation with a Pusher, for messages the server sends on its own, such as a broadcast.

Pooka
let echo = route.Route(fn(message) { [message] }, None, True)

API reference

class

Transport

src/whiplash/websocket/route.pooka:28
class Transport(
  read: fn(Int) -> Result(BitArray, String),
  write: fn(BitArray) -> Result(Nil, String)
)

A conversation needs only ordered bytes in and out. HTTP/1 supplies a socket; HTTP/2 supplies flow-controlled DATA and a bounded input channel. wait reads with a deadline and passes on what came; over HTTP/1 a quiet conversation hibernates meanwhile, so it must be the reader's last call.

field

read

src/whiplash/websocket/route.pooka:29
read: fn(Int) -> Result(BitArray, String)
INFERREDfn(Int) -> Result(BitArray, String)
field

write

src/whiplash/websocket/route.pooka:30
write: fn(BitArray) -> Result(Nil, String)
INFERREDfn(BitArray) -> Result(Nil, String)
constructor

new

src/whiplash/websocket/route.pooka:29
INFERREDfn(fn(Int) -> Result(BitArray, String), fn(BitArray) -> Result(Nil, String)) -> whiplash/websocket/route.Transport
method

wait

src/whiplash/websocket/route.pooka:32
wait(timeout: Int, resume: fn(Result(BitArray, String)) -> Nil) -> Nil
INFERREDfn(Int, fn(Result(BitArray, String)) -> Nil) -> Nil
class

Route

src/whiplash/websocket/route.pooka:60
class Route(
  on_message: fn(ws.Message) -> List(ws.Message),
  protocol: Option(String),
  compression: Bool
)

A WebSocket conversation: the reply to each message, the subprotocol to select (only if the client offered it), and whether to accept permessage-deflate when the client asks. Without an acceptable offer, messages go uncompressed.

field

on_message

src/whiplash/websocket/route.pooka:61
on_message: fn(ws.Message) -> List(ws.Message)
field

protocol

src/whiplash/websocket/route.pooka:62
protocol: Option(String)
INFERREDOption(String)
field

compression

src/whiplash/websocket/route.pooka:63
compression: Bool
INFERREDBool
constructor

new

src/whiplash/websocket/route.pooka:61
method

opened

src/whiplash/websocket/route.pooka:66
opened() -> Option(fn(Pusher) -> Nil)
INFERREDfn() -> Option(fn(whiplash/websocket/route.Pusher) -> Nil)

The function to run beside the conversation, if any.

method

with_open

src/whiplash/websocket/route.pooka:76
with_open(open: fn(Pusher) -> Nil) -> Route

Run open in a process linked to the conversation once the upgrade succeeds. It receives a Pusher: messages that the server sends on its own, such as a broadcast or a clock. Pushed messages are never compressed (RFC 7692 lets any message go uncompressed), so they do not disturb the conversation's compression context. Over HTTP/1 the pusher writes to the socket; over HTTP/2 its frames join the stream's DATA.

class

Pusher

src/whiplash/websocket/route.pooka:102
class Pusher(post: fn(ws.Message) -> Result(Nil, String))

Sends text, binary and ping messages to one client. A failed send means the client has gone: drop the pusher. A message pushed while the closing handshake runs may still be written just before the Close frame.

field

post

src/whiplash/websocket/route.pooka:102
post: fn(ws.Message) -> Result(Nil, String)
INFERREDfn(whiplash/websocket.Message) -> Result(Nil, String)
constructor

new

src/whiplash/websocket/route.pooka:102
INFERREDfn(fn(whiplash/websocket.Message) -> Result(Nil, String)) -> whiplash/websocket/route.Pusher
method

send

src/whiplash/websocket/route.pooka:103
send(message: ws.Message) -> Result(Nil, String)
INFERREDfn(whiplash/websocket.Message) -> Result(Nil, String)
class

Incoming

src/whiplash/websocket/route.pooka:110
class Incoming

What an HTTP/2 conversation reads from its connection: the peer's bytes, or a frame the application pushed, to be written in turn.

class

Input

src/whiplash/websocket/route.pooka:112
class Input(bytes: BitArray) is Incoming
field

bytes

src/whiplash/websocket/route.pooka:112
bytes: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket/route.pooka:112
INFERREDfn(BitArray) -> whiplash/websocket/route.Input
class

Pushed

src/whiplash/websocket/route.pooka:114
class Pushed(frame: BitArray) is Incoming
field

frame

src/whiplash/websocket/route.pooka:114
frame: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket/route.pooka:114
INFERREDfn(BitArray) -> whiplash/websocket/route.Pushed
function

pusher

src/whiplash/websocket/route.pooka:117
pusher(write: fn(BitArray) -> Result(Nil, String)) -> Pusher
INFERREDfn(fn(BitArray) -> Result(Nil, String)) -> whiplash/websocket/route.Pusher

A Pusher writing each message, as an uncompressed frame, through write.

function

serve

src/whiplash/websocket/route.pooka:137
serve(
  client: socket.Socket,
  method: String,
  headers: List(#(String, String)),
  bytes: BitArray,
  route: Route,
  max_message: Int,
  timeout: Int
) -> Nil
INFERREDfn(whiplash/socket.Socket, String, List(#(String, String)), BitArray, whiplash/websocket/route.Route, Int, Int) -> Nil

The initial HTTP bytes have already been checked and bounded by HTTP/1.

function

connect_stream

src/whiplash/websocket/route.pooka:376
connect_stream(
  headers: List(#(String, String)),
  route: Route,
  read: fn(Int) -> Result(Incoming, String),
  max_message: Int,
  timeout: Int
) -> Result(body.Stream, ws.WebsocketError)
INFERREDfn(List(#(String, String)), whiplash/websocket/route.Route, fn(Int) -> Result(whiplash/websocket/route.Incoming, String), Int, Int) -> Result(whiplash/body.Stream, whiplash/websocket.WebsocketError)

An extended CONNECT answers with 200, then carries WebSocket frames in DATA. There are no HTTP/1 Upgrade/Connection/Accept fields (RFC 8441 4).