yada

borkdude 2018-09-27T09:44:34.000100Z

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.

2018-09-27T10:19:07.000100Z

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)

2018-09-27T10:24:30.000100Z

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 😄

2018-09-27T10:26:04.000100Z

(sorry for the dirty code above btw - that’s the bane of my personal projects 😛)

borkdude 2018-09-27T10:59:47.000100Z

@nha I’m using the sente alpeh adapter yeah

borkdude 2018-09-27T11:00:38.000100Z

I have the same exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class manifold.stream.SplicedStream:

2018-09-27T12:06:03.000100Z

Right it should work for websockets though

dominicm 2018-09-27T12:22:28.000100Z

@borkdude we use a polyfill to support SSE in IE.

dominicm 2018-09-27T12:22:51.000100Z

This way we get authentication AND browser coverage

borkdude 2018-09-27T12:24:16.000100Z

cool, I’ll try that maybe. I don’t reeeally need SSE/websockets, but it would be nice to have

2018-09-27T13:46:44.000100Z

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).

2018-09-27T13:48:20.000100Z

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 🤪)