Forget About Constant Polling: Meet Server-Sent Events in Kotlin
\n\nImagine: you open a stock exchange terminal, and quotes update by themselves. Or you log into a site admin panel, and there's a live feed of the number of online users. No "Refresh" buttons, no endless requests to the server. Just a stream of data flowing in real time.
\n\nIn the world of web development, Server-Sent Events (SSE) are responsible for this — a protocol that allows the server to independently send data to the client over a single HTTP connection. And today I'll show you how to implement SSE in Kotlin using the Ktor framework. This won't be dry theory, but a full-fledged working example from server to client.
\n\nWhat are Server-Sent Events and Why Are They Cool?
\n\nOnce upon a time, to get fresh data, developers used polling. The client would knock on the server every N seconds: "So, any news?" The server would reply: "No." And so on in a loop. This is wildly inefficient: 99% of requests are empty, and the server wastes resources processing unnecessary connections.
\n\nSSE solves the problem radically: the client opens one connection, and the server itself sends data when it appears. It's like subscribing to news — you just wait, and they bring you the latest issue.
\n\nAdvantages of Server-Sent Events in Kotlin:
\n- \n
- Simplicity — works over regular HTTP, requires no additional libraries on the client (except the built-in EventSource in the browser). \n
- Automatic reconnection — if the connection breaks, the browser reconnects on its own. \n
- Resource savings — one connection instead of dozens. \n
- Native support in Kotlin — with Ktor and coroutines, writing an SSE server is a pleasure. \n
The main downside? SSE is a one-way channel (only from server to client). If you need two-way communication, look into WebSocket. But for notifications, news feeds, quotes, monitoring — SSE is ideal.
\n\nWhen Should You Use SSE in Your Kotlin Project?
\n\n- \n
- Notification feed — new messages, comments, likes. \n
- Monitoring and logs — streaming server logs in real time. \n
- Stock exchange data — updating currency, stock, and cryptocurrency rates. \n
- Task progress — report on file upload or data processing. \n
- Alert systems — emergency signals, device statuses. \n
If you are looking for a way to transfer updates from server to client without a headache — SSE Kotlin is your choice. Let's move on to the code.
\n\nProject Setup: Ktor + SSE
\n\nFirst, let's create a new Ktor project. I'll use Gradle Kotlin DSL. In build.gradle.kts we add dependencies:
val ktor_version = "2.3.7"\n\ndependencies {\n implementation("io.ktor:ktor-server-core:$ktor_version")\n implementation("io.ktor:ktor-server-netty:$ktor_version")\n implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")\n implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")\n implementation("ch.qos.logback:logback-classic:1.4.14")\n implementation("io.ktor:ktor-server-cors:$ktor_version")\n}\n\nNote: for SSE we don't need a separate library — Ktor supports streaming data out of the box via coroutines.
\n\nCreating an SSE Endpoint
\n\nNow for the interesting part — let's write a server that will send events to the client every 2 seconds. This will be a simulation of stock quotes.
\n\nimport io.ktor.server.application.*\nimport io.ktor.server.engine.*\nimport io.ktor.server.netty.*\nimport io.ktor.server.response.*\nimport io.ktor.server.routing.*\nimport kotlinx.co