Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

KotlinadvancedbackendAI generated

Real-time Chat Server with Ktor WebSockets

Build a chat server with Ktor WebSockets that can run as several instances behind a load balancer: users connect to any instance, join rooms and chat in real time, and Redis pub/sub makes sure a message sent on one instance reaches room members connected to the others. Each connection is handled with Kotlin coroutines, slow clients get bounded buffers so they can't slow anyone else down, dead connections are detected with pings, and clients reconnect and catch up on missed messages. You'll load test several instances to prove messages flow between them. You'll learn Ktor WebSockets, coroutines and channels, Redis pub/sub for scaling out, presence across instances, backpressure, and load testing.

Estimate
~14h
Steps
7
Completed by
0
Proposed by
codeseed.app

Ktor · WebSockets · Redis · Coroutines

Project roadmap

  1. 01

    Build a single-instance server

    ~2.5h

    Create a Ktor server with the WebSockets plugin (with ping and timeout configured) and a /chat route that requires a username. Define JSON messages with kotlinx.serialization (join, leave, message, history and presence), validate them, and broadcast messages to the room's connected sessions. Keep the last 50 messages per room for new members. Done when: several browser or CLI clients chat in rooms on one instance.

  2. 02

    Manage rooms and users safely

    ~2h

    Track rooms and their sessions in a structure that's safe with many coroutines (for example a ConcurrentHashMap of rooms to sets of sessions, or a single coroutine owning the state and receiving commands through a Channel). Give each session its own bounded outgoing Channel and a sender coroutine, so a slow client's buffer fills and it's disconnected instead of blocking others. Done when: a deliberately slow client is disconnected without affecting the rest.

  3. 03

    Connect instances with Redis pub/sub

    ~2.5h

    Publish every accepted message to a Redis channel per room (with a client such as Lettuce using its coroutine API), and have each instance subscribe to the rooms its users are in and deliver received messages locally, ignoring messages it published itself. Store room history in Redis (a capped list) so every instance serves the same history. Done when: users on two different instances in the same room see each other's messages.

  4. 04

    Track presence across instances

    ~1.5h

    Store who is in each room in Redis (a set per room, plus each instance's heartbeat key with an expiry, so users of a crashed instance are cleaned up), and publish join and leave events so every instance can show the full member list. Done when: the member list is correct across instances, and users of a stopped instance disappear within a short time.

  5. 05

    Handle reconnects

    ~1.5h

    Give every message an id (from a Redis counter per room). When a client reconnects, it sends the last message id it saw and receives the messages it missed from history before live messages. Write a small client that reconnects with increasing waits. Done when: disconnecting a client for a while and reconnecting delivers every missed message once.

  6. 06

    Load test across instances

    ~2h

    Run three instances behind nginx (with sticky connections not required) and Redis with Docker Compose, and write a load test (for example with Gatling or a Kotlin client using many coroutines) connecting thousands of clients to random instances and rooms. Check every message reaches every room member exactly once, and record message latency and throughput in README.md. Done when: README.md shows the results, and no messages are lost.

  7. 07

    Test it

    ~2h

    Write tests with Ktor's testApplication and WebSocket client: joining and messaging, validation, history, slow client handling, and two server instances in the same test sharing a Redis from Testcontainers. Done when: all tests pass.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~14h · 7 steps

Tech stack

KtorWebSocketsRedisCoroutines