sync handlers
murtaza52 opened this issue · 3 comments
Hi,
I see that the example code requires an async handler. Can it use a sync handler too, or is that a problem ?
Thanks,
Murtaza
Hello!
I think the very idea of Server Sent Events involves asynchrony. So it's not clear how to make it work with sync handler. You have to send some initial headers, keep connection open and then somehow send events which occur later. Async handler was designed for exactly this scenario.
Hi,
Maybe my understanding is incorrect. I thought the async handler in ring is just to make the handling of the operation async - ie Java servelet 3.0. - in this the HTTP server is running in a thread pool, and when it receives a request it hands it over to another thread which handles that particular request. So the server never gets tied up and is always avalaible for request handling.
While SSE corresponds to the IO being async - ie servelet 3.1 - in this case the IO itself is served asyncronously in bits and pieces - so websockets and SSE would fit this bill.
I am not sure which case is ring implementing.
If ring is implementing the first case (which is my understanding), then we dont need an async handler. As I understand in your implementation the async is being provided by go blocks, which park when input is not available.
However if ring is implementing the second case, that means the callback function is being called with data whenever it becomes avalaible, and there the async case makes sense.
What is your understanding ?
Thanks,
Murtaza
Hi,
I want to use in luminus-framework in RESTAPI, because I think sse is also a http portocol, my code like this :
["/sse"
{:get {:summary "sse-demo"
:parameters {}
:handler (fn []
(sse/event-channel-handler
(fn [request response raise event-ch]
(async/go
(dotimes [i 20]
(let [event {:id (java.util.UUID/randomUUID)
:name "foo"
:data (json/write-str {:foo "bar"})}]
(async/>! event-ch event)
(async/<! (async/timeout 1000))))
(async/close! event-ch)))
{:on-client-disconnect #(log/info :sse/on-client-disconnect %)})
)}}]
But it is failed, the error is :
clojure.lang.ArityException: Wrong number of args (1) passed to: project.routes.public/public-routes/fn--48140
at clojure.lang.AFn.throwArity(AFn.java:429) [clojure-1.10.0.jar:na]
at clojure.lang.AFn.invoke(AFn.java:32) [clojure-1.10.0.jar:na]
at project.middleware.log_interceptor$log_wrap$fn__36578.invoke(log_interceptor.clj:22) ~[na:na]
at reitit.ring.coercion$fn__34215$fn__34217$fn__34218.invoke(coercion.cljc:34) ~[na:na]
at muuntaja.middleware$wrap_format_request$fn__36185.invoke(middleware.clj:114) ~[na:na]
at reitit.ring.middleware.exception$wrap$fn__36329$fn__36330.invoke(exception.clj:49) ~[na:na]
at muuntaja.middleware$wrap_format_response$fn__36189.invoke(middleware.clj:132) [na:na]
at muuntaja.middleware$wrap_format_negotiate$fn__36182.invoke(middleware.clj:96) [na:na]
at ring.middleware.params$wrap_params$fn__8946.invoke(params.clj:67) [na:na]
at reitit.ring$ring_handler$fn__33905.invoke(ring.cljc:288) [na:na]
at clojure.lang.AFn.applyToHelper(AFn.java:154) [clojure-1.10.0.jar:na]
at clojure.lang.AFn.applyTo(AFn.java:144) [clojure-1.10.0.jar:na]
at clojure.lang.AFunction$1.doInvoke(AFunction.java:31) [clojure-1.10.0.jar:na]
at clojure.lang.RestFn.invoke(RestFn.java:408) [clojure-1.10.0.jar:na]
at ring.middleware.reload$wrap_reload$fn__4595.invoke(reload.clj:39) [na:na]
at selmer.middleware$wrap_error_page$fn__4610.invoke(middleware.clj:18) [na:na]
at prone.middleware$wrap_exceptions$fn__4867.invoke(middleware.clj:159) [na:na]
at ring.middleware.flash$wrap_flash$fn__4910.invoke(flash.clj:39) [na:na]
at immutant.web.internal.undertow$wrap_undertow_session$fn__54205.invoke(undertow.clj:72) [na:na]
at ring.middleware.cors$handle_cors.invokeStatic(cors.cljc:178) [na:na]
at ring.middleware.cors$handle_cors.invoke(cors.cljc:167) [na:na]
at ring.middleware.cors$wrap_cors$fn__7266.invoke(cors.cljc:205) [na:na]
at ring.middleware.keyword_params$wrap_keyword_params$fn__8566.invoke(keyword_params.clj:53) [na:na]
at ring.middleware.nested_params$wrap_nested_params$fn__8624.invoke(nested_params.clj:89) [na:na]
at ring.middleware.multipart_params$wrap_multipart_params$fn__8922.invoke(multipart_params.clj:173) [na:na]
at ring.middleware.params$wrap_params$fn__8946.invoke(params.clj:67) [na:na]
at ring.middleware.cookies$wrap_cookies$fn__8399.invoke(cookies.clj:175) [na:na]
at ring.middleware.absolute_redirects$wrap_absolute_redirects$fn__9132.invoke(absolute_redirects.clj:47) [na:na]
at ring.middleware.resource$wrap_resource_prefer_resources$fn__8982.invoke(resource.clj:25) [na:na]
at ring.middleware.content_type$wrap_content_type$fn__9080.invoke(content_type.clj:34) [na:na]
at ring.middleware.default_charset$wrap_default_charset$fn__9104.invoke(default_charset.clj:31) [na:na]
at ring.middleware.not_modified$wrap_not_modified$fn__9046.invoke(not_modified.clj:61) [na:na]
at ring.middleware.x_headers$wrap_x_header$fn__7295.invoke(x_headers.clj:22) [na:na]
at ring.middleware.x_headers$wrap_x_header$fn__7295.invoke(x_headers.clj:22) [na:na]
at ring.middleware.x_headers$wrap_x_header$fn__7295.invoke(x_headers.clj:22) [na:na]
at clojure.lang.Var.invoke(Var.java:384) [clojure-1.10.0.jar:na]
at immutant.web.internal.undertow$create_http_handler$reify__54316.handleRequest(undertow.clj:239) [na:na]
at org.projectodd.wunderboss.web.undertow.async.websocket.UndertowWebsocket$2.handleRequest(UndertowWebsocket.java:109) [wunderboss-web-undertow-0.13.1.jar:na]
at io.undertow.server.session.SessionAttachmentHandler.handleRequest(SessionAttachmentHandler.java:68) [undertow-core-1.4.14.Final.jar:1.4.14.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:211) [undertow-core-1.4.14.Final.jar:1.4.14.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:809) [undertow-core-1.4.14.Final.jar:1.4.14.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_192]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_192]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_192]
user>
In this framework,what do i still to do?
Thinks very much