Fork me on GitHub
#yada
<
2015-11-12
>
nha09:11:36

Hello, does yada support transit requests ? Also, is there an example of a yada component somewhere ?

nha12:11:01

Ok the component was straightforward. As for the transit requests, I'm not there yet. Also out of curiosity I see in the bidi readme that liberator is supported, is it also possible with yada ? If yes, how easy would it be ?

ordnungswidrig13:11:20

transit requests? you mean transit encoded data a request body?

nha13:11:35

(or response of the appropriate header is set - I mean can the data be exchanged in transit format? )

nha14:11:25

Also, I am trying to sent SSE in a response :

;; from aleph example
(defn stream-example
  [{:keys [params] :as ctx}]
  (let [cnt (Integer/parseInt (get params "count" "0"))]
    {:status 200
     :headers {"content-type" "text/plain"}
     :body (let [sent (atom 0)]
             (->> (s/periodically 100 #(str (swap! sent inc) "\n"))
                  (s/transform (take cnt))))}))


;; another test
(defn stream-example [req]
  (yada
   :allow-origin true
   :body {"text/event-stream"
          (fn [_]
            (let [ch (chan 256)]
              (go
                (>! ch "It works!"))
              ch))}))


(def api-routes
  ["" [["/hello-api"
        (yada/swaggered
         {:info {:title "Hello World!"
                 :version "1.0"
                 :description "Demonstrating yada + swagger"}
          :basePath "/hello-api"}
         ["/hello" hello])]
       ["/chat"
        (yada/swaggered
         {:info {:title "Chat service"
                 :version "1.0"
                 :description "test"}
          :basePath "/chat"}
         ["/stream-example" stream-example])]
       [true (fn [req] {:status 404 :body "Nada!"})]]])

(two different tries). does someone spot what is wrong ?

stijn15:11:06

it requires you to use a core async channel though, but if you like to work with manifold streams you could replace the mult with a manifold event-bus I think

nha15:11:11

I am ok with a core.async channel, I tried that in my second attempt above.

nha15:11:19

Not sure what I missed though

mitchelkuijpers15:11:13

(defn state-event-stream [event-bus cron _]
  {:headers {"content-type" "text/event-stream"}
   :body (stream/transform (map (partial format "data: %s\n\n"))
                           (stream/->source (state cron event-bus)))})

mitchelkuijpers15:11:36

And state returns a core.async channel

mitchelkuijpers15:11:51

but stream/->source also accepts manifold streams

mitchelkuijpers15:11:59

@nha: Maybe this helps?

nha16:11:02

Honestly I am not sure how to use that ?

mitchelkuijpers19:11:51

@nha sorry I just copy pasted something from a project where we use this.

mitchelkuijpers19:11:03

@nha your :body should return this: (transform (map (partial format "data: %s\n\n")) (->source ch)) where ch is your channel.

mitchelkuijpers19:11:31

See the example from @stijn

stijn20:11:17

@nha what version of Yada are you using? Because the :body {"text/event-stream"... is from an old version if I'm not mistaken. The body should be a manifold stream instead