WebSocket in Kotlin: Building Real-Time Applications from Scratch

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why WebSocket and Kotlin are the Perfect Pair for Real-Time

Modern web applications increasingly require instant data transmission: from chats and online games to stock quotes and monitoring systems. Traditional HTTP with its request-response model is inefficient for such tasks — it creates delays and redundant traffic. This is where WebSocket comes to the rescue — a protocol that provides a permanent two-way connection between client and server.

Kotlin with its concise syntax, full compatibility with Java, and powerful support for coroutines is ideally suited for creating real-time applications. The Ktor framework from JetBrains provides built-in WebSocket support, allowing you to write performant and safe code in just a few lines.

In this article, we will break down how to create a full-fledged real-time application in Kotlin using WebSocket. You will learn:

  • How to set up a WebSocket server on Ktor
  • How to connect to it from a browser or mobile application
  • How to organize real-time message exchange
  • Which patterns and approaches to use for scaling

1. WebSocket Basics and Its Advantages in Kotlin

What is WebSocket?

WebSocket is a protocol that works over TCP. After establishing a connection via an HTTP handshake (upgrade), the client and server can exchange data in both directions without repeated requests. This drastically reduces latency and decreases network load.

Why Kotlin?

  • Coroutines — allow handling thousands of simultaneous WebSocket connections without blocking threads.
  • Ktor — a lightweight asynchronous framework with native WebSocket support.
  • Type safety — errors at compile time, not at runtime.
  • Multiplatform — the same code can run on JVM, JS, and Native.

2. Creating a WebSocket Server on Ktor

Step 1: Project Setup

Add dependencies to build.gradle.kts:

dependencies {
    implementation("io.ktor:ktor-server-core:2.3.0")
    implementation("io.ktor:ktor-server-netty:2.3.0")
    implementation("io.ktor:ktor-server-websockets:2.3.0")
    implementation("ch.qos.logback:logback-classic:1.4.11")
}

Step 2: Basic Server with WebSocket

import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.websocket.*
import io.ktor.websocket.*
import kotlinx.coroutines.delay

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(WebSockets)
        webSocket("/chat") {
            send(Frame.Text("Welcome to the chat!"))
            for (frame in incoming) {
                if (frame is Frame.Text) {
                    val text = frame.readText()
                    println("Received: $text")
                    send(Frame.Text("Echo: $text"))
                }
            }
        }
    }.start(wait = true)
}

This code creates a simple echo server. Upon connection, the client receives a greeting, and each of its messages is returned with the prefix "Echo:".

Step 3: Handling Multiple Clients and Broadcasting

For a real-time chat, you need to store a list of active sessions:

import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.websocket.*
import io.ktor.websocket.*
import kotlinx.coroutines.channels.ConcurrentHashMap

val clients = ConcurrentHashMap<String, WebSocketSession>()

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(WebSockets)
        webSocket("/chat") {
            val userName = "User_${System.currentTimeMillis()}"
            clients[userName] = this
            try {                

Blogs

Book Recommendations