Fork me on GitHub
#ring
<
2017-05-16
>
fabrao16:05:55

Hello all, is there any way to do async response of a route?

fabrao16:05:38

(GET "/login" {params :params} (valid-login (params :username) (params :password)))
-> calls an ajax request and handles async response

fabrao16:05:00

how to handle with that?

fabrao16:05:50

(defn valid-login [usuario senha]
  (ajax/ajax-request
   {:uri "https://******/login?$format=JSON"
    :method :post
    :params {:username usuario
             :password senha}
    :format (ajax/url-request-format)
    :response-format (ajax/json-response-format {:keywords? true})
    :error-handler #(response {:resposta "OK"})
    :handler #(response {:resposta "OK"})
    })
  )

weavejester17:05:04

@fabrao Yes, you return a three-argument handler, like:

weavejester17:05:54

(GET "/login" [username password]
  (fn [_ respond _]
    (respond (valid-login username password))))

weavejester17:05:09

That’s using respond synchronously, but you can pass it as a callback.

weavejester17:05:35

Or extend the compojure.response/Sendable protocol for channels or some other asynchronous abstraction.

fabrao17:05:20

@weavejester I tested it but it´s getting "class ajax.apache$to_clojure_future$reify__72385 is not understandable"

weavejester17:05:46

Do you have the :async? option set on the adapter? Are you using Compojure 1.6.0 and Ring 1.6.1? Are you trying to return some kind of custom async object?

fabrao17:05:50

[ring/ring-defaults "0.2.1"] [ring/ring-json "0.5.0-beta1"] [buddy/buddy-auth "1.4.1"] [compojure "1.5.0"]

fabrao17:05:22

how do I set :async?

weavejester17:05:51

You need to upgrade your dependencies, and you need to set :async? true to your Jetty adapter.

fabrao17:05:00

I´m using figwheel ring handler

fabrao17:05:43

is that possible to set :async? true to ring-handler?

weavejester17:05:05

Not sure. It might be an option. Take a look through the docs.

weavejester17:05:20

I need to go. bbl