Guide
WebSockets
WebSockets
The second argument of Application picks a WebSocket route for a request, or
None. A route is a message callback, an optional subprotocol, and whether to
negotiate permessage-deflate:
module main
import pooka/http
import pooka/otp
import whiplash
import whiplash/websocket as ws
import whiplash/websocket/route
echo_route(message: ws.Message) -> List(ws.Message) {
match message {
ws.TextMessage(_) -> [message]
ws.BinaryMessage(_) -> [message]
_ -> []
}
}
main() {
let app = whiplash.Application(fn(_) { http.text(404, "Not found") }, fn(request) {
match request.path {
"/echo" -> Some(route.Route(echo_route, None, True))
_ -> None
}
})
let assert Ok(_) = whiplash.start_application(whiplash.options(8080), app)
otp.sleep_forever()
}
The callback returns the messages to send back, in order. Pings are answered
and the closing handshake is completed for you; the conversation ends when
either side closes, when the connection drops, or after request_timeout
without a message. max_body bounds each message, compressed or not.
Pushing
route.with_open(fn(pusher) { ... }) runs beside the conversation, in a
process linked to it. pusher.send(message) sends text, binary or ping
messages on the server's own initiative: a clock, a broadcast. Pushed messages
are never compressed, so they leave the conversation's compression state
untouched. A send that fails means the client has gone.
Transports
The same routes serve an HTTP/1.1 upgrade, HTTP/2 extended CONNECT (RFC 8441)
and HTTP/3 extended CONNECT (RFC 9220); pushers work on all three. Browsers
use HTTP/1.1 for ws:// URLs.
Conformance
Autobahn's fuzzing client passes every case over HTTP/1.1 and through nghttpx to HTTP/2: 517 cases, 505 OK, 9 non-strict (a connection closes at once on an invalid frame, before echoing frames that came before it in the same read), 3 informational.