chickadee » schematra » websocket

(websocket path clause ...)syntax

Register a WebSocket route at path. The route syntax is the same family as get/post/sse. Each clause matches an event; all are optional except on-text:

  • (on-open body ...) — runs once when the handshake completes.
  • (on-text message body ...) — runs for each text frame. message is bound to the decoded string payload.
  • (on-binary bytes body ...) — runs for each binary frame. bytes is the raw byte string.
  • (on-close code reason body ...) — runs when the connection closes, regardless of who initiated it. code is the WebSocket close code (1000 for normal, 1006 for an abrupt disconnect, etc.) and reason is the human-readable string.
  • (on-error exn body ...) — runs if a handler raises an exception, before the connection is closed. Use it for logging, metrics, or notifying other clients.

You can omit on-binary, on-error, or both, and there's a single-clause form for echo-style routes:

(websocket "/echo"
  (on-text message (send-text message)))