livery_ws (livery v0.2.0)

View Source

WebSocket integration on top of the ws library.

A handler that wants to upgrade a request to WebSocket calls upgrade/3 inside its body. The function performs the protocol-specific handshake by dispatching to the adapter's accept_ws/4 helper. The return value is a sentinel response (status = 101, body = taken_over) that tells livery:emit/3 no further bytes need to be written: the stream/socket now belongs to the ws session.

my_ws_route(Req) ->
    livery_ws:upgrade(Req, my_chat_handler, #{}).

my_chat_handler is a module implementing the ws_handler behaviour (defined by erlang_ws).

WebSocket runs over H1 (plain Upgrade), H2 (RFC 8441 extended CONNECT, via livery_ws_h2), and H3 (RFC 9220 extended CONNECT, via livery_ws_h3).

Summary

Functions

Upgrade the current request to a WebSocket session.

Types

handler_module()

-type handler_module() :: module().

handler_opts()

-type handler_opts() :: term().

Functions

upgrade(Req, HandlerMod, Opts)

Upgrade the current request to a WebSocket session.

HandlerMod must implement the ws_handler behaviour. Opts is opaque and forwarded as HMod:init(Req, Opts)'s second argument by the ws library.

Returns a #livery_resp{} value:

  • status = 101, body = taken_over on a successful handshake. The adapter owns nothing further on this stream after this point.
  • status = 400 with a textual body when the inbound headers do not satisfy RFC 6455.
  • status = 501 when the adapter does not support WebSocket upgrades (H1, H2, and H3 all do).