taoensso / sente

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

Callback reply is :chsk-timeout even though server replied #401

Closed njerig closed 1 year ago

njerig commented 2 years ago

I've cloned the sente repo to my local machine to play around with the example project. I noticed that when I click button 2, the callback reply is :chsk-timeout. Shouldn't the callback reply be the data sent by the server on the channel? Why is the reply :chsk-timeout if the server responds to the click with a push on :example/button2?

ptaoussanis commented 2 years ago

@njerig Hi Njeri, have you made any modifications to the reference example project?

If not, can you please share the output from your lein deps :tree? Thanks!

abishek commented 11 months ago

Am facing this issue in my code. I get an warning message WARN [taoensso.sente:1307] - Cb reply w/o local cb-fn: :chsk/bad-package

I am not using uid and the server is actually responding using the ?reply-fn (although there is a bit of promise resolution happening before the response). how do I debug this?

ptaoussanis commented 11 months ago

@abishek Hi Abishek, as I mentioned above - it'd be helpful to know:

abishek commented 11 months ago

@ptaoussanis thank you for the quick response. I am not using the same example. Let me try to see if I can share the code (I might need to create a simpler project to try first). And I am not using leiningen. My deps.edn looks like this:

{:paths ["src" "test" "resources"]
 :deps {org.clojure/clojure       {:mvn/version "1.11.1"}
        org.clojure/clojurescript {:mvn/version "1.11.60"}
        org.clojure/core.async    {:mvn/version "1.6.673"}
        thheller/shadow-cljs      {:mvn/version "2.24.1"}
        ;; macchiato framework for the backend
        macchiato/core            {:mvn/version "0.2.24"} 
        macchiato/hiccups         {:mvn/version "0.4.1"}
        cljs-http/cljs-http       {:mvn/version "0.1.46"}
        ;; router for both front and backends
        metosin/reitit            {:mvn/version "0.7.0-alpha5"}
        ;; reagent for the frontend.
        reagent/reagent           {:mvn/version "1.2.0"}
        ;; logger
        com.taoensso/timbre       {:mvn/version "6.2.1"}
        ;; websockets
        com.taoensso/sente        {:mvn/version "1.17.0"}
        ;; diagramming tool
        quil/quil                 {:mvn/version "4.0.0-SNAPSHOT-1"}
        ;; integrant for state management
        integrant/integrant       {:mvn/version "0.8.1"}}}

fwiw, am not using integrant.

The client side function is like this:

(defn get-map []
  (ws/chsk-send! [:ros/get-map] 6000 (fn [resp] (logger/info resp))))

(defn say-hello 
  []
  [:button {:on-click get-map} "Get The Map"])

(defn mount-app
  []
  (logger/info "mounting app module.")
  (rdom/render [say-hello] (.getElementById js/document "app")))

And on the server side, it looks like this

(defmethod -event-message-handler :ros/get-map
  [{:as ev-msg :keys [id ?reply-fn]}]
  (if ?reply-fn
    (ros/fetch-map ?reply-fn)
    (logger/warnf "%s expects a callback function." id)))

the ros/fetch-map takes the callback function as an argument and then calls the function inside a promise resolution + callback. Let me try to share that code in a separate repo.

I pretty much use your example project for most of Sente setup. I wrote this project to understand how to use sente, so my current setup code is exactly the same as there.

Thanks!

ptaoussanis commented 11 months ago

@abishek Hi Abishek,

It's generally not feasible for me to go through an entire project to help someone debug - especially if they're using libraries and/or frameworks that I'm not familiar with.

This is why I usually recommend that step 1 for debugging, is to check the reference example to see if the problem is reproducible there.

If it's reproducible there, there's either a bug with Sente - or an issue with your environment.

If it's not reproducible there, then you can do a diff between the reference example and your own project to try and identify changes that could be causing a problem.

Very often, folks will realise this issue just looking by looking at the reference example.

In your particular case the error message "WARN [taoensso.sente:1307] - Cb reply w/o local cb-fn: :chsk/bad-package" indicates that there's an error in the underlying pack format used to relay data between Sente's client and server.

That could be because of a client-server version mismatch, a faulty packer is being used, etc.

The particular cause could probably be most easily identified by comparison to the reference example.

Hope that seems reasonable!

abishek commented 11 months ago

@ptaoussanis I really didn't expect you to go through the project! But that said, I think the pointer to the packer helps. I didn't know what to look for. I have initialised the packer to :edn on server and client sides. But the data I callback with is a json object. Can I use json as a packer? I am a bit of a newb to this library, so do point me at possible packer solutions to try. thanks again for the quick response.

ptaoussanis commented 11 months ago

I have initialised the packer to :edn on server and client sides.

That's a reasonable choice 👍

But the data I callback with is a json object. Can I use json as a packer?

I'm not sure what you mean by "the data I callback with is a json object", but you shouldn't need to use a JSON packer to send JSON in event content. JSON's just a string, the default (edn) packer would have no problem sending JSON.

Just make sure to read the string as JSON in your relevant handlers.

I am a bit of a newb to this library, so do point me at possible packer solutions to try.

You really shouldn't have any need to adjust the packer. I would recommend taking a look at the various example projects and other info on the wiki to get started.

Would also recommend you use that latest stable version of Sente (currently 1.18.1), it looks like you're on 1.17 which has known bugs that have been addressed in later releases.

abishek commented 11 months ago

@ptaoussanis thanks. let me check out the examples again. I'll also look from whether am missing some other aspect of the data that am dealing with as I am fetching it from another library. Thanks for the tips here :-)

I tried using sente 1.18.1. But when I use it with macchiato project inside a nodejs server, I get an error TypeError: goog.global.addListener is not a function. The error points to taoensso/sente.cljc:1304:4 which based in git blame seems to have changed recently. So I went back to 1.17 just to get past that :-)

I didn't spend enough time to see if that's related to sente or related to something am missing in my setup. I'll try that again and maybe open a new ticket later if I can't figure it out. Thanks!