Invalid event "~#chsk/handshake" when using sente packers.transit/get-transit-packer in clojure

210 views Asked by At

I am getting an error when using sente that results in failure while sending information from client to server or vice versa. The problem seems to be that the handshake fails with an error

cljs$core$ExceptionInfo
  message: "Invalid event"
  data: {:given "~:chsk/handshake", :errors {:wrong-type {…}}}

The successive ws/ping also fail with the same error but with extra information,

sente.cljc:142 Uncaught #error {:message "Invalid event", :data {:given "~#'", :errors {:wrong-type 
{…}[:expected :vector]
   [:actual {:type #object[String], :value "~#'"}]

What might be the problem, my code seems okay and follows the default example in sente.

Expected behaviour:

Sente would connect and i would be able to call send-fn and send messages between the server and client, successfully.

Edit: Addition of code as suggested in the comments:

(require '[taoensso.sente.server-adapters.aleph :refer (get-sch-adapter)])
;; Create Websocket connection in server
(let [packer (sente-transit/get-transit-packer)
      chsk-server (sente/make-channel-socket-server! (get-sch-adapter) {:packer packer
                                                                        :csrf-token-fn nil})
      {:keys [ch-recv send-fn connected-uids ajax-post-fn ajax-get-or-ws-handshake-fn]} chsk-server]
  (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)
  (def chsk-send! send-fn)
  (def connected-uids connected-uids))
;; Start the web server
(defn start-web-server! [& [port]]
  (stop-web-server!)
  (let [port (or port default-port)
        ring-handler (var main-ring-handler)
        [port stop-fn]
        (let [server (aleph/start-server ring-handler {:port port})
              p (promise)]
          (future @p)
          [(aleph.netty/port server)
           (fn [] (.close ^Closeable server) (deliver p nil))])
        uri (format "http://localhost:%s/" port)]
    (infof "Web server is running at `%s`" uri)
    (reset! web-server_ {:port port :stop-fn stop-fn})
    (try
      (if (and (Desktop/isDesktopSupported)
               (.isSupported (Desktop/getDesktop) Desktop$Action/BROWSE))
        (.browse (Desktop/getDesktop) (URI. uri))
        (.exec (Runtime/getRuntime) (str "xdg-open" uri)))
      (Thread/sleep 7500)
      (catch HeadlessException _))))

On the client side:

(let [packer (sente-transit/get-transit-packer)
      {:keys [chsk ch-recv send-fn state]}
      (sente/make-channel-socket-client! "/chsk"
                                         {:type   :auto
                                          :packer packer})]
  (def chsk chsk)
  (def ch-chsk ch-recv)
  (def chsk-send! send-fn)
  (def chsk-state state))
;; start the router
(defn start-router! []
  (stop-router!)
  (reset! router_ (sente/start-client-chsk-router! ch-chsk event-msg-handler)))

EDIT, Addition

I have noted that the error only exists when i use the packer, (packers.transit/get-transit-packer) and not :edn

2

There are 2 answers

3
bbs On BEST ANSWER

Ran into this issue today coincidentally, so after this issue I was scratching my head a bit, could it be the library which hasn't seen much activity/is stable?

Nope. I simply forgot to include the [com.cognitect/transit-cljs "0.8.256"] cljs dependency!

The example did warn: https://github.com/ptaoussanis/sente/blob/master/example-project/src/example/client.cljs#L47

0
Jochen Bedersdorfer On

Not exactly an answer, but we gotta see your code for that. The error "Invalid event" is generated here:

(defn validate-event
  "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 _}}`."
  [x]
  (cond
    (not (vector? x))        {:wrong-type   (expected :vector x)}
    (not (#{1 2} (count x))) {:wrong-length (expected #{1 2}  x)}
    :else
    (let [[ev-id _] x]
      (cond
        (not (keyword? ev-id))  {:wrong-id-type   (expected :keyword            ev-id)}
        (not (namespace ev-id)) {:unnamespaced-id (expected :namespaced-keyword ev-id)}
        :else nil))))
    ```