Server-sent events (SSE) don't work with CIO server
See original GitHub issueKtor Version
1.2.2
Ktor Engine Used
ktor-server-cio
JVM Version, Operating System and Relevant Context
$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
Feedback
When trying to produce server-sent events, response body doesn’t get sent to client. If I start server like this:
package cz.scuk.app
import io.ktor.application.call
import io.ktor.http.CacheControl
import io.ktor.http.ContentType
import io.ktor.response.cacheControl
import io.ktor.response.respondTextWriter
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import kotlinx.coroutines.delay
fun main() {
embeddedServer(CIO, port = 8080) {
routing {
get("/") {
call.response.cacheControl(CacheControl.NoCache(null))
call.respondTextWriter(contentType = ContentType.Text.EventStream) {
while (true) {
write("data: ${System.currentTimeMillis()}\n\n")
flush()
delay(1000)
}
}
}
}
}.start(true)
}
And then send request with curl:
$ curl -v --no-buffer http://localhost:8080/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Transfer-Encoding: chunked
< Content-Type: text/event-stream; charset=UTF-8
<
The curl hangs, no response is being received.
When CIO
implementation is replaced with Netty
, client periodically receives data chunks. Also when delay(1000)
is removed, data are also sent to the client.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Using server-sent events - Web APIs | MDN
The server-sent event API is contained in the EventSource interface; to open a connection to the server to begin receiving events from it, ......
Read more >How to Use Server-Sent Events (SSE) With FastAPI
Server -sent events (SSE) is a way to send data to the browser without reloading the page.
Read more >Working with Server-Sent Events in Swift - Nick Arner
As long as that connection remains open, the client will be aware of any changes that have occurred server-side. In this post, I'll...
Read more >What Server-Sent Events is - and how and when to implement it
In a nutshell, a server-sent event is when updates are pushed (rather than pulled, or requested) from a server to a browser.
Read more >Server-sent event does not work with jersey SSE
I had the same problem and solved it by not setting the name of the event (i don't know why but this seems...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just out of curiosity, why not?
Fixed in 1.5.2