Module Spindle_http.Websocket

WebSockets, served and called, in one model: a protocol says what each side sends, and each end holds a socket typed from its own side.

type said = { text : string } [@@deriving wiretype]

let chat =
  Websocket.(protocol ~client:(json said_json) ~server:(json said_json) ())

let echo =
  Spindle.get path (Returns.websocket chat)
    (Dep.return
       (Ok
          (fun ws ->
            let rec echo () =
              let* said = Websocket.receive ws in
              let* () = Websocket.send ws said in
              echo ()
            in
            echo ())))

let greet client =
  Spindle_client.websocket client chat "wss://example.com/echo" (fun ws ->
      let* () = Websocket.send ws { text = "hi" } in
      Websocket.receive ws)

The framework runs RFC 6455 whole -- the frames, their masks and fragments, ping and pong, keep-alive, the closing handshake -- and a handler only receives, sends and, if it wants to, closes. Every failure is a value: a handler returns a result, so a loop written with let* ends at the first thing that went wrong and says which, and how it ended decides the close. Nothing here raises into the application, and nothing cancels a handler whose socket has gone: it learns at its next receive or send.

What a socket carries

type 'a message = private
  1. | Json : 'a Wiretype.t -> 'a message
    (*

    JSON of a description, as text

    *)
  2. | Text : string message
    (*

    text as it is

    *)
  3. | Binary : string message
    (*

    bytes as they are

    *)

What travels one way, made by json, text or binary; the constructors are for whatever describes a protocol, which reads them.

val json : 'a Wiretype.t -> 'a message
val text : string message
val binary : string message
type ('c, 's) protocol

What a client sends, 'c, and what a server sends, 's.

val protocol : ?subprotocol:string -> client:'c message -> server:'s message -> unit -> ('c, 's) protocol

subprotocol is Sec-WebSocket-Protocol, which both ends then insist on: a server refuses a client that did not offer it, and a client one that did not choose it.

val subprotocol : (_, _) protocol -> string option
val client : ('c, _) protocol -> 'c message
val server : (_, 's) protocol -> 's message

A socket

type (!'i, !'o) t

An open socket, from one end: it receives 'i and sends 'o. A route's socket is ('c, 's) t, a caller's ('s, 'c) t. It belongs to the fibers of the domain that runs it.

type close_code =
  1. | Normal
    (*

    1000

    *)
  2. | Going_away
    (*

    1001: a server stopping, a page closed

    *)
  3. | Protocol_error
    (*

    1002

    *)
  4. | Unsupported
    (*

    1003: a message of a kind this end does not take

    *)
  5. | Invalid_data
    (*

    1007: a message that could not be read

    *)
  6. | Policy
    (*

    1008

    *)
  7. | Too_big
    (*

    1009

    *)
  8. | Internal
    (*

    1011: our bug

    *)
  9. | Code of int
    (*

    any other a peer may send -- 1005 for a close that named none, 1010, 1012 to 1014, and 3000 to 4999, the application's own

    *)

Why a socket is closed, as RFC 6455 §7.4 numbers them.

type close = {
  1. code : close_code;
  2. reason : string;
}

A close, by either end: its code and reason.

type error = [
  1. | `Socket of [ `Closed of close ]
    (*

    closed, by either end: the socket's own state, under its name

    *)
  2. | `Lost of string
    (*

    the connection ended without a close, or went silent, or stopped taking what was written: in words for the log

    *)
  3. | `Undecodable of string
    (*

    a message its description could not read, in words for the log; the socket is still open

    *)
  4. | `Unwritable of string
    (*

    a message its description could not write -- a value its JSON description cannot encode, text that is not UTF-8 (§8.1) -- in words for the log; nothing was sent, and the socket is still open

    *)
]

How a socket's work failed, in the words every Hypha library uses, so a handler's let* runs over a socket and a statement alike: what a call answers is [> error], open to the handler's own.

val error_to_string : [< error ] -> string
val receive : ('i, _) t -> ('i, [> error ]) Stdlib.result

The next whole message, waiting for it. Once the socket has ended, its ending, every time.

val send : (_, 'o) t -> 'o -> (unit, [> error ]) Stdlib.result

Answers once the message is written. A peer that takes nothing written for the connection's send_timeout makes it `Lost; one still taking it, however slowly, is sent the whole. A value its description cannot write -- one JSON cannot encode, text that is not UTF-8 -- is `Unwritable, and nothing is sent. A send cancelled -- by a timeout, or an Eio.Fiber.first it lost -- may have written part of its frame, which the peer would read the next frame into, so it ends the socket as `Lost and the cancellation goes on: every later send answers `Lost, and nothing more is written.

type 'o encoded

A message encoded once, to be sent to many sockets: what a Spindle.Broadcast of sockets carries, so the publisher renders once.

val encode : 'o message -> 'o -> 'o encoded

A value its description cannot write makes a message every send of which answers `Unwritable.

val send_encoded : (_, 'o) t -> 'o encoded -> (unit, [> error ]) Stdlib.result
val close : ?code:close_code -> ?reason:string -> (_, _) t -> unit

Begins the closing handshake with code (Normal) and reason (none). What follows is read as ever, and receive answers `Socket (`Closed _) once the peer has answered. A code no endpoint may send is our bug: logged, and the socket closed with Internal. So is a reason longer than 123 bytes or not UTF-8, which no close can carry (§5.5, §8.1): logged, and the close sent without it.