Anyone got an example on how to integrate yada and sente? I’ve looked at SSE but we need to support IE/Edge. I got:
:get
{:parameters {:query {:handshake? s/Bool
:udt String
:client-id String
s/Any s/Any}}
:response (fn [ctx]
(let [query (get-in ctx [:parameters :query])
req (update (:request ctx) :params merge query)
sente-response (ring-ajax-get-or-ws-handshake req)]
(:body sente-response)))}
but this is obviously not working.I’ve had trouble to make it work with yada and polling etc., although it works fine with sente websockets transport. (on a personal project though so I never had much time to dig around)
You’re not the first to ask in this channel though - I did the same quite some time ago. It took some trial and error and I have this at the moment:
(defn- ringify [ctx]
(update ctx :request (comp
ring.middleware.keyword-params/keyword-params-request
ring.middleware.params/params-request)))
(defn- handlers [{:keys [ajax-post-fn
ajax-get-or-ws-handshake-fn]}]
{::chsk (yada/resource {:description "Sente endpoints"
:consumes yada.util/consumes
:produces yada.util/produces
:methods {:get {:response (fn [ctx]
(def ctx ctx)
(let [req (:request (ringify ctx))
res (:response ctx)]
(let [result (ajax-get-or-ws-handshake-fn req)]
;; TODO assoc ctx
;; + look at headers etc.
;; for websockets
;; TODO see for Ajax too
(def result result)
(type result)
(println "NOT RETURNGING BODY - TODO AJAX" (:body result))
(assoc res :body (:body (deref result)))
;; com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class manifold.stream.SplicedStream: manifold.stream.SplicedStream@42d74aea
;; originalMessage: "Cannot JSON encode object of class: class manifold.stream.SplicedStream: manifold.stream.SplicedStream@42d74aea"
nil)))}
:post {:response (fn [ctx]
(let [req (ringify ctx)]
(assoc ctx :body (ajax-post-fn req))))}}})} )
note you have to use the sente aleph adapter too (not shown above).
My code looks a bit weird (still lots of debugging stuff left in there) - I would be very interested in seeing what you come up with @borkdude 😄(sorry for the dirty code above btw - that’s the bane of my personal projects 😛)
@nha I’m using the sente alpeh adapter yeah
I have the same exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class manifold.stream.SplicedStream:
Right it should work for websockets though
@borkdude we use a polyfill to support SSE in IE.
This way we get authentication AND browser coverage
cool, I’ll try that maybe. I don’t reeeally need SSE/websockets, but it would be nice to have
The odd thing is that it worked for me with bidi + compojure instead of yada for a resource handler. It did feel very clunky though (I don’t think I have sample code handy anymore though I will be able to dig a bit this weekend).
This and https://github.com/juxt/yada/issues/205 are basically the two things I miss from Yada (the issue is because I would love to reuse my existing Yada API for my websocket messages - without making an actual request to myself 🤪)