Fork me on GitHub
#yada
<
2018-09-27
>
borkdude09:09:34

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.

nha10:09:07

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)

nha10:09:30

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 😄

nha10:09:04

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

borkdude10:09:47

@nha I’m using the sente alpeh adapter yeah

borkdude11:09:38

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

nha12:09:03

Right it should work for websockets though

dominicm12:09:28

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

dominicm12:09:51

This way we get authentication AND browser coverage

borkdude12:09:16

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

nha13:09:44

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

nha13:09:20

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