taoensso.sente

Channel sockets for Clojure/Script.

    Protocol  | client>server | client>server ?+ ack/reply | server>user push
  * WebSockets:       ✓              [1]                           ✓
  * Ajax:            [2]              ✓                           [3]

  [1] Emulate with cb-uuid wrapping
  [2] Emulate with dummy-cb wrapping
  [3] Emulate with long-polling

Abbreviations:
  * chsk      - Channel socket (Sente's own pseudo "socket")
  * server-ch - Underlying web server's async channel that implement
                Sente's server channel interface
  * sch       - server-ch alias
  * uid       - User-id. An application-level user identifier used for async
                push. May have semantic meaning (e.g. username, email address),
                may not (e.g. client/random id) - app's discretion.
  * cb        - Callback
  * tout      - Timeout
  * ws        - WebSocket/s
  * pstr      - Packed string. Arbitrary Clojure data serialized as a
                string (e.g. edn) for client<->server comms
  * udt       - Unix timestamp (datetime long)

Special messages:
  * Callback wrapping: [<clj> <?cb-uuid>] for [1], [2]
  * Callback replies: :chsk/closed, :chsk/timeout, :chsk/error

  * Client-side events:
      [:chsk/ws-ping] ; ws-ping from server
      [:chsk/handshake [<?uid> nil[4] <?handshake-data> <first-handshake?>]]
      [:chsk/state     [<old-state-map> <new-state-map> <open-change?>]]
      [:chsk/recv      <ev-as-pushed-from-server>] ; Server>user push

  * Server-side events:
      [:chsk/ws-ping] ; ws-ping from client
      [:chsk/ws-pong] ; ws-pong from client
      [:chsk/uidport-open  <uid>]
      [:chsk/uidport-close <uid>]
      [:chsk/bad-package   <packed-str>]
      [:chsk/bad-event     <event>]

Channel socket state map:
  :type               - e/o #{:auto :ws :ajax}
  :open?              - Truthy iff chsk appears to be open (connected) now
  :ever-opened?       - Truthy iff chsk handshake has ever completed successfully
  :first-open?        - Truthy iff chsk just completed first successful handshake
  :uid                - User id provided by server on handshake,    or nil
  :handshake-data     - Arb user data provided by server on handshake
  :last-ws-error      - ?{:udt _ :ev <WebSocket-on-error-event>}
  :last-ws-close      - ?{:udt _ :ev <WebSocket-on-close-event>
                          :clean? _ :code _ :reason _}
  :last-close         - ?{:udt _ :reason _}, with reason e/o
                          #{nil :clean :unexpected :requested-disconnect
                            :requested-reconnect :downgrading-ws-to-ajax
                            :ws-ping-timeout :ws-error}
  :udt-next-reconnect - Approximate udt of next scheduled auto-reconnect attempt

Notable implementation details:
  * core.async is used liberally where brute-force core.async allows for
    significant implementation simplifications. We lean on core.async's
    efficiency here.
  * For WebSocket fallback we use long-polling rather than HTTP 1.1 streaming
    (chunked transfer encoding). Http-kit _does_ support chunked transfer
    encoding but a small minority of browsers &/or proxies do not. Instead of
    implementing all 3 modes (WebSockets, streaming, long-polling) - it seemed
    reasonable to focus on the two extremes (performance + compatibility).
    In any case client support for WebSockets is growing rapidly so fallback
    modes will become increasingly irrelevant while the extra simplicity will
    continue to pay dividends.

General-use notes:
  * Single HTTP req+session persists over entire chsk session but cannot
    modify sessions! Use standard a/sync HTTP Ring req/resp for logins, etc.
  * Easy to wrap standard HTTP Ring resps for transport over chsks. Prefer
    this approach to modifying handlers (better portability).

[4] Used to be a csrf-token. Was removed in v1.14 for security reasons.
A `nil` remains for limited backwards-compatibility with pre-v1.14 clients.

*simulated-bad-conn-rate*

dynamic

clj

cljs

Debugging tool. Proportion ∈ℝ[0,1] of connection activities to sabotage.

*write-legacy-pack-format?*

dynamic

clj

cljs

Advanced option, most users can ignore this var. Only necessary
for those that want to use Sente < v1.18 with a non-standard
IPacker that deals with non-string payloads.

Details:
  Sente uses a private message format as an implementation detail
  for client<->server comms.

  As part of [#398], this format is being updated to support
  non-string (e.g. binary) payloads.

  Unfortunately updating the format is non-trivial because:
    1. Both the client & server need to support the same format.
    2. Clients are often served as cached cl/js.

  To help ease migration, the new pack format is being rolled out
  in stages:

    Sente <= v1.16: reads  v1 format only
                    writes v1 format only

    Sente    v1.17: reads  v1 and v2 formats
                    writes v1 and v2 formats (v1 default)

    Sente    v1.18: reads  v1 and v2 formats
                    writes v1 and v2 formats (v2 default)  <- Currently here

    Sente >= v1.19: reads  v2 format only
                    writes v2 format only

  This var controls which format to use for writing.
  Override default with `alter-var-root` or `binding`.

ajax-call

cljs

Alias of `taoensso.encore/ajax-call`

allow-origin?

clj

cljs

(allow-origin? allowed-origins ring-req)
Alpha, subject to change.
Returns true iff given Ring request is allowed by `allowed-origins`.
`allowed-origins` may be `:all` or #{<origin> ...}.

as-event

clj

cljs

(as-event x)

assert-event

clj

cljs

(assert-event x)
Returns given argument if it is a valid [ev-id ?ev-data] form. Otherwise
throws a validation exception.

cb-error?

cljs

(cb-error? cb-reply-clj)

cb-success?

cljs

(cb-success? cb-reply-clj)

ChAjaxSocket

cljs

ChAutoSocket

cljs

chsk-break-connection!

clj

cljs

(chsk-break-connection! chsk)(chsk-break-connection! chsk {:keys [close-ws?], :as opts, :or {close-ws? true}})
Breaks channel socket's underlying connection without doing a clean
disconnect as in `chsk-disconnect!`. Useful for simulating broken
connections in testing, etc.

Options:

  `:close-ws?` - (Default: true)
    Allow WebSocket's `on-close` event to fire?
    Set to falsey to ~simulate a broken socket that doesn't realise
    it's broken.

chsk-connect!

clj

cljs

(chsk-connect! chsk)

chsk-disconnect!

clj

cljs

(chsk-disconnect! chsk)

chsk-reconnect!

clj

cljs

(chsk-reconnect! chsk)
Cycles connection, useful for reauthenticating after login/logout, etc.

chsk-send!

clj

cljs

(chsk-send! chsk ev)(chsk-send! chsk ev ?timeout-ms ?cb)(chsk-send! chsk ev opts)
Sends `[ev-id ev-?data :as event]`, returns true on apparent success.

ChWebSocket

cljs

client-event-msg?

clj

cljs

(client-event-msg? x)

client-unloading?_

clj

cljs

EdnPacker

cljs

event-msg?

clj

cljs

event?

clj

cljs

(event? x)
Valid [ev-id ?ev-data] form?

IChSocket

protocol

clj

cljs

members

-chsk-send!

(-chsk-send! chsk ev opts)

-chsk-reconnect!

(-chsk-reconnect! chsk reason)

-chsk-disconnect!

(-chsk-disconnect! chsk reason)

-chsk-connect!

(-chsk-connect! chsk)

-chsk-break-connection!

(-chsk-break-connection! chsk opts)

make-channel-socket!

clj

cljs

Platform-specific alias for `make-channel-socket-server!` or
`make-channel-socket-client!`. Please see the appropriate aliased fn
 docstring for details.

make-channel-socket-client!

clj

cljs

(make-channel-socket-client! path ?csrf-token-or-fn & [{:as opts, :keys [type protocol host port params headers recv-buf-or-n packer ws-constructor ws-kalive-ms ws-ping-timeout-ms ws-opts client-id ajax-opts wrap-recv-evs? backoff-ms-fn], :or {ws-ping-timeout-ms 5000, ws-kalive-ms 20000, ws-constructor default-client-ws-constructor, client-id (or (:client-uuid opts) (enc/uuid-str)), packer :edn, type :auto, recv-buf-or-n (async/sliding-buffer 2048), backoff-ms-fn enc/exp-backoff, wrap-recv-evs? false}} _deprecated-more-opts])
Returns nil on failure, or a map with keys:
  :ch-recv ; core.async channel to receive `event-msg`s (internal or from
           ; clients). May `put!` (inject) arbitrary `event`s to this channel.
  :send-fn ; (fn [event & [?timeout-ms ?cb-fn]]) for client>server send.
  :state   ; Watchable, read-only (atom {:type _ :open? _ :uid _ :csrf-token _}).
  :chsk    ; IChSocket implementer. You can usu. ignore this.

Required arguments:
  path              ; Channel socket server route/path (typically `/chsk`)
  ?csrf-token-or-fn ; CSRF string or (fn [])->string to match token expected by server.
                    ; nil => server not expecting any CSRF token.

Common options:
  :type           ; e/o #{:auto :ws :ajax}. You'll usually want the default (:auto).
  :protocol       ; Server protocol, e/o #{:http :https}.
  :host           ; Server host (defaults to current page's host).
  :port           ; Server port (defaults to current page's port).
  :params         ; Map of any params to incl. in chsk Ring requests (handy
                  ; for application-level auth, etc.).
  :headers        ; Map of additional headers to include in the initiating request
                  ; (currently only for Java clients).
  :packer         ; :edn (default), or an IPacker implementation.
  :ajax-opts      ; Base opts map provided to `taoensso.encore/ajax-call`, see
                  ; relevant docstring for more info.
  :wrap-recv-evs? ; Should events from server be wrapped in [:chsk/recv _]?
                  ; Default false for Sente >= v1.18, true otherwise.

  :ws-kalive-ms       ; Ping to keep a WebSocket conn alive if no activity
                      ; w/in given msecs. Should be different to server's :ws-kalive-ms.
  :ws-ping-timeout-ms ; When pinging to test WebSocket connections, msecs to
                      ; await reply before regarding the connection as broken

  :ws-constructor ; Advanced, (fn [{:keys [uri-str headers on-message on-error on-close]}]
                  ; => nil, or delay that can be dereffed to get a connected WebSocket.
                  ; See `default-client-ws-constructor` code for details.

make-channel-socket-server!

clj

cljs

(make-channel-socket-server! web-server-ch-adapter & [{:keys [recv-buf-or-n ws-kalive-ms lp-timeout-ms ws-ping-timeout-ms send-buf-ms-ajax send-buf-ms-ws user-id-fn bad-csrf-fn bad-origin-fn csrf-token-fn handshake-data-fn packer allowed-origins authorized?-fn unauthorized-fn ?unauthorized-fn ms-allow-reconnect-before-close-ws ms-allow-reconnect-before-close-ajax], :or {ws-ping-timeout-ms nil, ws-kalive-ms (enc/ms :secs 25), send-buf-ms-ws 30, allowed-origins :all, lp-timeout-ms (enc/ms :secs 20), ms-allow-reconnect-before-close-ws 2500, csrf-token-fn (fn [ring-req] (or (:anti-forgery-token ring-req) (get-in ring-req [:session :csrf-token]) (get-in ring-req [:session :ring.middleware.anti-forgery/anti-forgery-token]) (get-in ring-req [:session "__anti-forgery-token"]))), packer :edn, ms-allow-reconnect-before-close-ajax 5000, unauthorized-fn (fn [_ring-req] {:status 401, :body "Unauthorized request"}), send-buf-ms-ajax 100, bad-origin-fn (fn [_ring-req] {:status 403, :body "Unauthorized origin"}), handshake-data-fn (fn [ring-req] nil), user-id-fn (fn [ring-req] (get-in ring-req [:session :uid])), recv-buf-or-n (async/sliding-buffer 1000), bad-csrf-fn (fn [_ring-req] {:status 403, :body "Bad CSRF token"})}}])
Takes a web server adapter[1] and returns a map with keys:

  :ch-recv ; core.async channel to receive `event-msg`s (internal or from clients).
  :send-fn                     ; (fn [user-id ev] for server>user push.
  :ajax-post-fn                ; Ring handler for CSRF-POST + chsk URL.
  :ajax-get-or-ws-handshake-fn ; Ring handler for Ring GET  + chsk URL.
  :connected-uids ; Watchable, read-only (atom {:ws #{_} :ajax #{_} :any #{_}}).

Security options:

  :allowed-origins   ; e.g. #{"http://site.com" ...}, defaults to :all. ; Alpha

  :csrf-token-fn     ; ?(fn [ring-req]) -> CSRF-token for Ajax POSTs and WS handshake.
                     ; nil => CSRF check will be DISABLED (can pose a *CSRF SECURITY RISK*
                     ; for website use cases, so please ONLY disable this check if you're
                     ; very sure you understand the implications!).

  :authorized?-fn    ; ?(fn [ring-req]) -> When non-nil, (authorized?-fn <ring-req>)
                     ; must return truthy, otherwise connection requests will be
                     ; rejected with (unauthorized-fn <ring-req>) response.
                     ;
                     ; May check Authroization HTTP header, etc.

  :?unauthorized-fn  ; An alternative API to `authorized?-fn`+`unauthorized-fn` pair.
                     ; ?(fn [ring-req)) -> <?rejection-resp>. I.e. when return value
                     ; is non-nil, connection requests will be rejected with that
                     ; non-nil value.

Other common options:

  :user-id-fn         ; (fn [ring-req]) -> unique user-id for server>user push.
  :handshake-data-fn  ; (fn [ring-req]) -> arb user data to append to handshake evs.
  :ws-kalive-ms       ; Ping to keep a WebSocket conn alive if no activity
                      ; w/in given msecs. Should be different to client's :ws-kalive-ms.
  :lp-timeout-ms      ; Timeout (repoll) long-polling Ajax conns after given msecs.
  :send-buf-ms-ajax   ; [2]
  :send-buf-ms-ws     ; [2]
  :packer             ; :edn (default), or an IPacker implementation.

  :ws-ping-timeout-ms ; When pinging to test WebSocket connections, msecs to
                      ; await reply before regarding the connection as broken

  ;; When a connection is closed, Sente waits a little for possible reconnection before
  ;; actually marking the connection as closed. This facilitates Ajax long-polling,
  ;; server->client buffering, and helps to reduce event noise from spotty connections.
  :ms-allow-reconnect-before-close-ws   ; Msecs to wait for WebSocket conns (default: 2500)
  :ms-allow-reconnect-before-close-ajax ; Msecs to wait for Ajax      conns (default: 5000)

[1] e.g. `(taoensso.sente.server-adapters.http-kit/get-sch-adapter)` or
         `(taoensso.sente.server-adapters.immutant/get-sch-adapter)`.
    You must have the necessary web-server dependency in your project.clj and
    the necessary entry in your namespace's `ns` form.

[2] Optimization to allow transparent batching of rapidly-triggered
    server>user pushes. This is esp. important for Ajax clients which use a
    (slow) reconnecting poller. Actual event dispatch may occur <= given ms
    after send call (larger values => larger batch windows).

sente-version

clj

cljs

Useful for identifying client/server mismatch

server-event-msg?

clj

cljs

(server-event-msg? x)

set-min-log-level!

clj

cljs

(set-min-log-level! level)
Sets Timbre's minimum log level for internal Sente namespaces.
Possible levels: #{:trace :debug :info :warn :error :fatal :report}.
Default level: `:warn`.

start-chsk-router!

clj

cljs

Platform-specific alias for `start-server-chsk-router!` or
`start-client-chsk-router!`. Please see the appropriate aliased fn
docstring for details.

start-client-chsk-router!

clj

cljs

(start-client-chsk-router! ch-recv event-msg-handler & [{:as opts, :keys [trace-evs? error-handler]}])
Creates a simple go-loop to call `(event-msg-handler <server-event-msg>)`
and log any errors. Returns a `(fn stop! [])`. Note that advanced users may
prefer to just write their own loop against `ch-recv`.

Nb performance note: since your `event-msg-handler` fn will be executed
within a simple go block, you'll want this fn to be ~non-blocking
(you'll especially want to avoid blocking IO) to avoid starving the
core.async thread pool under load. To avoid blocking, you can use futures,
agents, core.async, etc. as appropriate.

start-server-chsk-router!

clj

cljs

(start-server-chsk-router! ch-recv event-msg-handler & [{:as opts, :keys [trace-evs? error-handler simple-auto-threading?]}])
Creates a simple go-loop to call `(event-msg-handler <server-event-msg>)`
and log any errors. Returns a `(fn stop! [])`. Note that advanced users may
prefer to just write their own loop against `ch-recv`.

Nb performance note: since your `event-msg-handler` fn will be executed
within a simple go block, you'll want this fn to be ~non-blocking
(you'll especially want to avoid blocking IO) to avoid starving the
core.async thread pool under load. To avoid blocking, you can use futures,
agents, core.async, etc. as appropriate.

Or for simple automatic future-based threading of every request, enable
the `:simple-auto-threading?` opt (disabled by default).

validate-event

clj

cljs

(validate-event x)
Returns nil if given argument is a valid [ev-id ?ev-data] form. Otherwise
returns a map of validation errors like `{:wrong-type {:expected _ :actual _}}`.