taoensso / sente

Realtime web comms library for Clojure/Script
https://www.taoensso.com/sente
Eclipse Public License 1.0
1.74k stars 193 forks source link

use defonce in the examples #308

Closed thedavidmeister closed 1 year ago

thedavidmeister commented 7 years ago

Hi, I've been using sente in an app that uses boot and watch.

At some point, watch started causing my websockets to not be able to connect after I refresh the page.

It seemed to help using defonce instead of def in this code:

;;; Add this: --->
(let [{:keys [ch-recv send-fn connected-uids
              ajax-post-fn ajax-get-or-ws-handshake-fn]}
      (sente/make-channel-socket! (get-sch-adapter) {})]

  (def ring-ajax-post                ajax-post-fn)
  (def ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn)
  (def ch-chsk                       ch-recv) ; ChannelSocket's receive channel
  (def chsk-send!                    send-fn) ; ChannelSocket's send API fn
  (def connected-uids                connected-uids) ; Watchable, read-only atom
  )
Hendekagon commented 7 years ago

I prefer to put all server app state in an atom and then use this:

(defn add-sente! [state]
  (let [{:keys [ch-recv send-fn connected-uids
               ajax-post-fn ajax-get-or-ws-handshake-fn]}
       (sente/make-channel-socket! (get-sch-adapter) {})]
    (-> state
      (assoc-in [:sente :ring-ajax-post] ajax-post-fn)
      (assoc-in [:sente :ring-ajax-get-or-ws-handshake] ajax-get-or-ws-handshake-fn)
      (assoc-in [:sente :ch-chsk] ch-recv)                                 ; ChannelSocket's receive channel
      (assoc-in [:sente :chsk-send!] send-fn)                              ; ChannelSocket's send API fn
      (assoc-in [:sente :connected-uids] connected-uids)                   ; Watchable, read-only atom)
      )))

to add sente channels. I really don't like defing app state in a namespace.

ptaoussanis commented 1 year ago

Will be included in forthcoming v1.18 release, apologies for the long delay.