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

How to get information about why the first connection attempt has failed? #407

Closed dmg46664 closed 1 year ago

dmg46664 commented 1 year ago

Hi,

I'm porting client test code over to sente (because I was using a node.js lib and core.async) but the actual target will be react-native.

There is no evidence on the local server (already written with npm ws) that any connection attempt was made. ❌ (eventual server will be AWS gateway websockets backed by lambda fns) ⚠️ I'm more perplexed however that I can't find a way to see any error state on the client, or any evidence as to the nature of connection attempt.

My code is as follows:

(defn connect-web-socket! [url]
  (let [{:keys [host port path]} (url-to-map url)
        {:keys [chsk ch-recv send-fn state]}
        (sente/make-channel-socket-client!
         "/"
         nil
         {:type :ws ; e/o #{:auto :ajax :ws}
          :host "localhost"
          :port 8888 ;; tried "8888" as well
          })]
    (timbre/debug chsk)
    (add-watch state ::watcher (fn[key atom old-state new-state]
                                 (timbre/debug new-state)))
    {:=received= ch-recv
     :send-fn send-fn
     :chsk chsk
     :state state}
    )
  )

Apon calling this I can see the following coming from somewhere within sente but not failure message is in the logs.

2022-07-26T11:26:29.286Z WARN [taoensso.sente:1250] - Chsk is closed: will try reconnect attempt (1) in 1385 ms

followed by my debug message:

2022-07-26T11:26:29.287Z DEBUG [launch-client:38] - #taoensso.sente.ChWebSocket{:client-id "b31fd291-e455-46bd-b1fb-403ba8a73782", :chs {:internal #object[cljs.core.async.impl.channels.ManyToManyChannel], :state #object[cljs.core.async.impl.channels.ManyToManyChannel], :<server #object[cljs.core.async.impl.channels.ManyToManyChannel]}, :params nil, :headers nil, :packer #object[taoensso.sente.EdnPacker], :url "ws://localhost:8888/", :ws-kalive-ms 20000, :state_ #object[cljs.core.Atom {:val {:type :ws, :open? false, :ever-opened? false, :csrf-token nil, :udt-next-reconnect 1658834790671}}], :instance-handle_ #object[cljs.core.Atom {:val "54eca435-faec-4a45-831d-d3e08b5ce9a8"}], :retry-count_ #object[cljs.core.Atom {:val 1}], :ever-opened?_ #object[cljs.core.Atom {:val false}], :backoff-ms-fn #object[taoensso$encore$exp_backoff], :cbs-waiting_ #object[cljs.core.Atom {:val {}}], :socket_ #object[cljs.core.Atom {:val nil}], :udt-last-comms_ #object[cljs.core.Atom {:val nil}], :ws-opts nil}

Eventually the watch shows

2022-07-26T11:26:30.674Z WARN [taoensso.sente:1250] - Chsk is closed: will try reconnect attempt (2) in 3514 ms
2022-07-26T11:26:30.676Z DEBUG [launch-client:40] - {:type :ws, :open? false, :ever-opened? false, :csrf-token nil, :udt-next-reconnect 1658834794188}

but this doesn't give me the ability to look for the failed connection information. Only a change in state.

What am I doing wrong? TIA

P.S. Logging here as per https://github.com/ptaoussanis/sente/issues/406 your suggestion about node.js

ptaoussanis commented 1 year ago

@dmg46664 Hi Daniel,

There is no evidence on the local server (already written with npm ws) that any connection attempt was made.

Sorry, can you please clarify: it sounds like you're trying to connect a Sente client to a a non-Sente server? Or are you running make-channel-socket-server! somewhere? Is that running successfully?

dmg46664 commented 1 year ago

Thanks for a prompt response @ptaoussanis Yes, I am trying to connect Sente to a non-Sente server. Hopefully that clarifies. There is nothing on the readme page to suggest it isn't a bog-standard websocket api? (and what would be the value of creating a bespoke protocol so close to WebSockets?)

ptaoussanis commented 1 year ago

@dmg46664 No problem.

Yes, I am trying to connect Sente to a non-Sente server. Hopefully that clarifies.

Unfortunately Sente won't work in this case- a Sente client can connect only to a Sente server. That's why I was asking in your other issue what you were trying to do - since it surprised me to hear that you said you had no server.

There is nothing on the readme page to suggest it isn't a bog-standard websocket api?

I believe that the README and examples are hopefully reasonably clear that Sente is a client+server library that requires the server component? The Getting Started guide and examples all begin by starting a Sente server, and no support is claimed for the NodeJS WS library.

In any case, I understand that this must be frustrating news. I apologise if this could have been clearer, it hadn't occurred to me that someone might expect Sente to interop directly with NodeJS WS.

PR very welcome to help improve the README 👍

(and what would be the value of creating a bespoke protocol so close to WebSockets?)

As the README mentions, this will include things like the automatic buffering/batching, arbitrary Clojure value support, the built-in user-id mechanism, etc. Different libraries will inevitably offer different tradeoffs - Sente's objective was never interop with other servers, but a maximally pleasant experience for its common use case of Clojure server and Clojure clients.

Am not certain, but I guess there may be other lighter Clojure clients available that can interop directly with NodeJS WS.

Best of luck!

dmg46664 commented 1 year ago

someone might expect Sente to interop directly with NodeJS WS

Not NodeJS WS, but the WebSocket protocol.

Yip, that is very frustrating 😢

like the automatic buffering/batching, arbitrary Clojure value support, the built-in user-id mechanism, etc.

None of the features mentioned in the quoted list conflict fundamentally with Websockets and so the expectation was this provides convenience on top of the protocol.

dmg46664 commented 1 year ago

I thought the value add here was core.async on top of Websockets, and so I wouldn't have to worry about Java/cljs(node/browser) and would have one api here.

ptaoussanis commented 1 year ago

and so I wouldn't have to worry about Java/cljs(node/browser) and would have one api here

Sorry, still not sure I clearly understand what you're trying to accomplish. In any case, it does seem that Sente won't be useful in your case due to its custom protocol.

You may want to consider just using WebSockets directly if possible (often pretty easy if you don't want auto fallback and all the other features), or you could check if there's other libraries around more in line with your needs.

Closing for now, but PR welcome if you'd like to suggest any clarifications to the README.

Cheers :-)