question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server-sent events (SSE) don't work with CIO server

See original GitHub issue

Ktor 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
arkanoviczcommented, Jan 7, 2021

Could you please describe your use-case: why do you use SSE and not WebSockets?

Just out of curiosity, why not?

1reaction
e5lcommented, Apr 5, 2021

Fixed in 1.5.2

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found