Konstantin
01/28/2025, 7:25 AMimport io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.routing.routing
import io.ktor.server.sse.SSE
import io.ktor.server.sse.sse
import io.ktor.sse.ServerSentEvent
import kotlinx.coroutines.delay
fun main() {
embeddedServer(Netty, port = 8080) {
install(SSE)
routing {
sse("/sse") {
repeat(6) {
println("Sending SSE #$it")
send(ServerSentEvent("this is SSE #$it"))
delay(1000)
}
}
}
}.start(wait = true)
}
When I run it and try to consume, I get an empty response:
$curl -N -v -w "\nTotal time: %{time_total}s\n" <http://localhost:8080/sse>
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080
> GET /sse HTTP/1.1
> Host: localhost:8080
> User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0
> Accept: */*
>
* Request completely sent off
* Empty reply from server
* Closing connection
curl: (52) Empty reply from server
I see the server route was triggered, but only 2 events were sent, but neither HTTP headers nor SSE events were received:
[main] INFO io.ktor.server.Application - Responding at <http://0.0.0.0:8080>
Sending SSE #0
Sending SSE #1
What am I missing?Maria Skripchenko
01/28/2025, 2:46 PMcurl -N -v -w "\nTotal time: %{time_total}s\n" <http://localhost:8080/sse>
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /sse HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.86.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/event-stream
< Cache-Control: no-store
< Connection: keep-alive
< X-Accel-Buffering: no
< transfer-encoding: chunked
<
data: this is SSE #0
data: this is SSE #1
data: this is SSE #2
data: this is SSE #3
data: this is SSE #4
data: this is SSE #5
* Connection #0 to host localhost left intact
Total time: 6.161408s
And for server:
2025-01-28 15:45:27.208 [main] INFO io.ktor.server.Application - Application started in 0.223 seconds.
2025-01-28 15:45:27.335 [main] INFO io.ktor.server.Application - Responding at <http://0.0.0.0:8080>
Sending SSE #0
Sending SSE #1
Sending SSE #2
Sending SSE #3
Sending SSE #4
Sending SSE #5
Can you please provide more details?Konstantin
01/28/2025, 2:53 PMKonstantin
01/29/2025, 8:30 AMktor-serialization-kotlinx-json-jvm
dependency alone breaks things. Adding ktor-serialization-jackson-jvm
does not hurt.
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <http://maven.apache.org/xsd/maven-4.0.0.xsd>">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ktor-sample</artifactId>
<version>0.0.1</version>
<name>ktor-sample</name>
<description>ktor-sample</description>
<properties>
<kotlin.code.style>official</kotlin.code.style>
<kotlin_version>2.1.10</kotlin_version>
<ktor_version>3.0.3</ktor_version>
<logback_version>1.4.14</logback_version>
<slf4j_version>2.0.9</slf4j_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<main.class>com.example.ApplicationKt</main.class>
</properties>
<repositories>
</repositories>
<dependencies>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-core-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-sse-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-content-negotiation-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<!-- FIXME: this dependency breaks
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-serialization-kotlinx-json-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
-->
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-netty-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin_version}</version>
<configuration>
<jvmTarget>17</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin_version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin_version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
code:
import io.ktor.http.CacheControl
import io.ktor.http.ContentType
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.response.cacheControl
import io.ktor.server.response.respondTextWriter
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import io.ktor.sse.ServerSentEvent
import kotlinx.coroutines.delay
fun main() {
embeddedServer(Netty, port = 8080) {
routing {
get("{...}") {
call.response.cacheControl(CacheControl.NoCache(null))
call.respondTextWriter(contentType = ContentType.Text.EventStream) {
repeat(6) {
println("Sending SSE #$it")
write(ServerSentEvent("this is SSE #$it").toString())
flush()
delay(1000)
}
}
}
}
}.start(wait = true)
}
output:
$ curl -N -v -w "\nTotal time: %{time_total}s\n" <http://localhost:8080/sse>
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080
> GET /sse HTTP/1.1
> Host: localhost:8080
> User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Content-Type: text/event-stream; charset=UTF-8
< transfer-encoding: chunked
<
data: this is SSE #0
data: this is SSE #1
data: this is SSE #2
data: this is SSE #3
data: this is SSE #4
data: this is SSE #5
* Connection #0 to host localhost left intact
Total time: 6.046663s
Konstantin
01/29/2025, 8:31 AMAleksei Tirman [JB]
01/29/2025, 10:18 AM