Liking cljdoc? Tell your friends :D

supabase.realtime.connection

WebSocket connection lifecycle for Supabase Realtime.

Holds a single hato-backed WebSocket per connect call. State lives in one atom; mutation goes through swap!; user callbacks run outside the swap to avoid running user code under contention.

The Transport protocol is the test seam — tests substitute a recording transport (see realtime_test) without redefining hato internals.

WebSocket connection lifecycle for Supabase Realtime.

Holds a single hato-backed WebSocket per `connect` call. State lives in
one atom; mutation goes through `swap!`; user callbacks run outside the
swap to avoid running user code under contention.

The `Transport` protocol is the test seam — tests substitute a recording
transport (see `realtime_test`) without redefining hato internals.
raw docstring

add-binding!clj

(add-binding! conn topic binding)

Appends a binding to channel topic. Returns the new channel-state or nil.

Appends a binding to channel `topic`. Returns the new channel-state or nil.
sourceraw docstring

build-ws-urlclj

(build-ws-url client)
(build-ws-url client extra-params)

Returns the full Realtime WebSocket URL for client with params (default: apikey + vsn) merged into the query string.

Returns the full Realtime WebSocket URL for `client` with `params`
(default: `apikey` + `vsn`) merged into the query string.
sourceraw docstring

channel-stateclj

(channel-state conn topic)

Reads the current channel-state map for topic, or nil.

Reads the current channel-state map for `topic`, or nil.
sourceraw docstring

connectclj

(connect client)
(connect client opts)

Opens a Realtime connection for client.

Options

  • :on-error(fn [anomaly]) for async transport/server errors
  • :heartbeat-ms — heartbeat interval in ms (default 30000)
  • :params — extra query params merged into the WS URL
  • :transport-factory(fn [url headers handlers]) returning a Transport. Defaults to ws-transport. Useful for tests.
  • :auto-reconnect? — reconnect on unexpected close (default true)
  • :reconnect-after-ms(fn [tries]) → delay before attempt tries. Defaults to default-reconnect-after-ms.
  • :max-reconnect-attempts — give up after N attempts (default: never)
  • :access-token-fn — zero-arity fn returning a token string. Called for the initial upgrade and re-resolved at every reconnect and channel (re)join, so a refreshed token is picked up. On exception or non-string result, falls back to the token set via set-auth, then the client's :access-token, then :api-key.
  • :http-fallback? — send plain broadcasts over HTTP POST when the socket is not :open (default false). See supabase.realtime/broadcast.

Returns a connection map, or an anomaly on failure.

Opens a Realtime connection for `client`.

## Options

  - `:on-error`               — `(fn [anomaly])` for async transport/server errors
  - `:heartbeat-ms`           — heartbeat interval in ms (default 30000)
  - `:params`                 — extra query params merged into the WS URL
  - `:transport-factory`      — `(fn [url headers handlers])` returning a
                                `Transport`. Defaults to `ws-transport`.
                                Useful for tests.
  - `:auto-reconnect?`        — reconnect on unexpected close (default true)
  - `:reconnect-after-ms`     — `(fn [tries])` → delay before attempt `tries`.
                                Defaults to `default-reconnect-after-ms`.
  - `:max-reconnect-attempts` — give up after N attempts (default: never)
  - `:access-token-fn`        — zero-arity fn returning a token string.
                                Called for the initial upgrade and re-resolved
                                at every reconnect and channel (re)join, so a
                                refreshed token is picked up. On exception or
                                non-string result, falls back to the token set
                                via `set-auth`, then the client's
                                `:access-token`, then `:api-key`.
  - `:http-fallback?`         — send plain broadcasts over HTTP POST when the
                                socket is not `:open` (default false). See
                                `supabase.realtime/broadcast`.

Returns a connection map, or an anomaly on failure.
sourceraw docstring

default-reconnect-after-msclj

(default-reconnect-after-ms tries)

Default backoff: min(10s, 2s^tries) — 2s, 4s, 8s, then 10s cap. Matches realtime-ex.

Default backoff: `min(10s, 2s^tries)` — 2s, 4s, 8s, then 10s cap.
Matches realtime-ex.
sourceraw docstring

disconnectclj

(disconnect conn)

Closes the connection: stops the heartbeat and reconnect timer, closes the socket, and marks state as :closed. Idempotent; no reconnect is scheduled.

Closes the connection: stops the heartbeat and reconnect timer, closes the
socket, and marks state as `:closed`. Idempotent; no reconnect is
scheduled.
sourceraw docstring

dispatch-frameclj

(dispatch-frame conn frame)

Routes a decoded inbound frame to the matching state-update + user callbacks. Pure mechanism; user callbacks run after state updates.

Routes a decoded inbound `frame` to the matching state-update + user
callbacks. Pure mechanism; user callbacks run after state updates.
sourceraw docstring

enqueue!clj

(enqueue! conn frame)

Buffers a frame if the socket isn't open yet, otherwise sends it now. Returns true on send, false on buffer.

Buffers a frame if the socket isn't open yet, otherwise sends it now.
Returns true on send, false on buffer.
sourceraw docstring

remove-channel!clj

(remove-channel! conn topic)

Drops topic from the channels map.

Drops `topic` from the channels map.
sourceraw docstring

resolve-tokenclj

(resolve-token conn)

Resolves the bearer token for conn: the :access-token-fn result when supplied and successful, then the last token set via set-auth (cleared on sign-out), then the client's :access-token, then :api-key.

Resolves the bearer token for `conn`: the `:access-token-fn` result when
supplied and successful, then the last token set via `set-auth` (cleared
on sign-out), then the client's `:access-token`, then `:api-key`.
sourceraw docstring

set-auth-token!clj

(set-auth-token! conn token)

Records token as the connection's current auth token (nil clears it, e.g. on sign-out). Later channel joins and reconnect rejoins resolve the token via resolve-token, so a cleared token no longer leaks into join payloads. Mirrors realtime-js updating the join payload on auth change.

Records `token` as the connection's current auth token (nil clears it,
e.g. on sign-out). Later channel joins and reconnect rejoins resolve the
token via `resolve-token`, so a cleared token no longer leaks into join
payloads. Mirrors realtime-js updating the join payload on auth change.
sourceraw docstring

Transportcljprotocol

close!clj

(close! this code reason)

Initiates an orderly close of the underlying connection.

Initiates an orderly close of the underlying connection.

send-textclj

(send-text this text)

Sends text as a single text frame. Returns truthy on success or throws/returns an exception object on failure.

Sends `text` as a single text frame. Returns truthy on success or
throws/returns an exception object on failure.
source

update-channel!clj

(update-channel! conn topic f & args)

Applies f (with extra args) to the channel-state at topic. Returns the new channel-state, or nil if the topic isn't tracked.

Applies `f` (with extra `args`) to the channel-state at `topic`. Returns
the new channel-state, or nil if the topic isn't tracked.
sourceraw docstring

upsert-channel!clj

(upsert-channel! conn topic config)

Initializes channel-state for topic if absent. Returns the updated channel-state.

Initializes channel-state for `topic` if absent. Returns the updated
channel-state.
sourceraw docstring

ws-transportclj

(ws-transport url headers handlers)

Opens a hato WebSocket to url with the given upgrade headers and handlers map. Returns a reified Transport.

handlers keys: :on-open (fn []) :on-text (fn [text]) :on-close (fn [code reason]) :on-error (fn [throwable])

Partial text frames are buffered until last? is true.

Opens a hato WebSocket to `url` with the given upgrade `headers` and
`handlers` map. Returns a reified `Transport`.

`handlers` keys:
  :on-open    (fn [])
  :on-text    (fn [text])
  :on-close   (fn [code reason])
  :on-error   (fn [throwable])

Partial text frames are buffered until `last?` is true.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close