Fork me on GitHub
Ian Fernandez22:12:50

anyone has an example with reitit + sente?

Ian Fernandez22:12:03

I couldn't configure it right

Noah Bogart22:12:01

(defn api-routes []
    [["/chsk" {:get ws/handshake-handler
               :post ws/post-handler}]
and my sente handlers are straight from the examples

Noah Bogart22:12:50

well, slightly:

(let [chsk-server (sente/make-channel-socket-server!
                    {:user-id-fn (fn [ring-req]
                                   (or (-> ring-req :session :uid)
                                       (:client-id ring-req)))})
      {:keys [ch-recv send-fn
              ajax-post-fn ajax-get-or-ws-handshake-fn]} chsk-server]
  (defonce handshake-handler ajax-get-or-ws-handshake-fn)
  (defonce post-handler ajax-post-fn)
  (defonce ch-chsk ch-recv)
  (defn chsk-send! [uid ev] (send-fn uid ev)))

clojure-spin 1
Ian Fernandez22:12:20

This needs to be in this way??? why not something like:

(def sente-config
  (let [packer (sente-transit/->TransitPacker :json
                                              {:handlers {}}
        {:keys [ch-recv
                ajax-get-or-ws-handshake-fn]} (sente/make-channel-socket-server!
                                                {:packer packer})]
    (add-watch connected-uids :connected-uids
               (fn [_ _ old new]
                 (when (not= old new)
                   (infof "Connected uids change: %s" new))))
    {:ring-ajax-post                ajax-post-fn
     :ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn
     ; ChannelSocket's receive channel
     :ch-chsk                       ch-recv
     ; ChannelSocket's send API fn
     :chsk-send!                    send-fn
     ; Watchable, read-only atom
     :connected-uids                connected-uids}))

Noah Bogart22:12:50

that seems roughly equivalent, sure, but you gotta make sure to call (sente/start-server-chsk-router! ch-chsk event-msg-handler) at some point, where event-msg-handler is my multimethod that dispatches on :id

Noah Bogart22:12:43

or i guess in your case (sente/start-server-chsk-router! (:ch-chsk sente-config) event-msg-handler)

Ian Fernandez23:12:52

Where is the (sente/start-server-chsk-router! (:ch-chsk sente-config) event-msg-handler) ???

Ian Fernandez23:12:57

is in the main???

Noah Bogart23:12:57

Sure! I have it in an integrant method but you can also just plop it as a top level form as a side effect if you want

πŸ‘ 1
Noah Bogart23:12:21

Check out the bottom of the server file in the official example repo

Noah Bogart22:12:44

my chsk-send! is explicit so I can get linting from clj-kondo, but it’s equivalent to (def chsk-send! send-fn)

πŸ‘ 1
Noah Bogart22:12:43

client code:

(def ?csrf-token
  (when-let [el (.getElementById js/document "sente-csrf-token")]
    (.getAttribute el "data-csrf-token")))

(if-not ?csrf-token
  (println "CSRF token NOT detected in HTML, default Sente config will reject requests")
  (let [{:keys [ch-recv send-fn]}
          {:type :auto
           :wrap-recv-evs? false})]
    (def ch-chsk ch-recv)
    (defn ws-send!
      ([ev] (send-fn ev))
      ([ev ?timeout ?cb] (send-fn ev ?timeout ?cb)))))

Noah Bogart22:12:21

and then the starting and stopping are directly from the examples too

πŸ‘ 1