Realtime chat bugs developers fix in TypeScript
Chat feels simple until the network gets involved. Responses come back in a different order from the one they were sent in, connections drop for a few seconds, and the user is looking at one channel while messages arrive in another. This guide covers the bugs that come up most in a realtime chat client, with examples from Pulse, Kadra's TypeScript chat practice codebase.
Sort by the server's order, not arrival order
If you send three messages quickly, the responses can come back 1, 3, 2. A client that appends messages as responses arrive shows them in that order. That's Pulse's RT-601 ticket.
The server decides the order, usually with an increasing sequence number. Sort on it every time the list changes:
return [...messages, incoming].sort(byServerOrder);Optimistic messages make this harder. The message you just typed appears straight away, before the server has given it a sequence number. When the server's copy arrives, replace the local one, matched by a client-generated id, so it moves to its proper place and doesn't appear twice.
Don't count messages the user is looking at
An unread badge should count messages the user hasn't seen. In RT-607 the count only resets when you switch channels, so a message that arrives while you're already reading that channel still bumps the badge.
if (msg.channelId === focusedChannelId) {
return state; // the user can see it, so it isn't unread
}Test three cases: opening a channel clears its count, a message in the open channel doesn't raise it, and a message in a background channel still does.
Backfill what you missed after a reconnect
When a socket drops and reconnects, it only gets messages sent from then on. Anything sent during the gap is lost, and nothing tells the user. RT-613, the senior ticket in Pulse, asks the client to fetch what it missed.
Keep the sequence number of the last message you saw. After reconnecting, ask for everything after it, drop anything you already have, and sort:
const missed = serverMessages
.filter((m) => m.seq !== null && m.seq > state.lastSeenSeq)
.sort((a, b) => a.seq - b.seq);
const seen = new Set(state.messages.map((m) => m.id));
const toAppend = missed.filter((m) => !seen.has(m.id));Subscribe to live events before you fetch the backfill. If you fetch first, a message sent between the fetch and the subscribe falls into a second gap. Subscribing first can deliver a message twice instead, and the id check above handles that.
Practice on a chat client
Pulse is a TypeScript realtime chat app with the RT tickets above planted in it. You get your own GitHub repo, fix each ticket on a branch and open a pull request. An AI teammate reviews the diff, and a hidden Vitest suite decides whether the fix passes.
Every Pulse repo opens in GitHub Codespaces with Node 24 and npm installed, so there's nothing to set up on your own machine.
Questions people ask
What is an optimistic update in a chat app?
Showing the user's message straight away, before the server confirms it, so the app feels instant. The client then has to swap in the server's copy, or show an error if the send failed.
WebSockets or server-sent events for chat?
WebSockets if the client sends often over the same connection. Server-sent events are simpler when the server does most of the sending, and the client can post messages with ordinary requests. Either one needs a way to catch up after a reconnect.
How do I test reconnect logic?
Keep the logic in plain functions that take the current state and the server's messages and return the new state. Then a test can play out a disconnect, a gap and a reconnect without opening a socket.
Is Kadra free?
Yes, Kadra is free during beta with no credit card. After beta, Pro is planned at $12 a month or $79 a year, and the demo and your first full sprint stay free.
Try it on a real ticket
Pick a stack, fix a bug in a working codebase and get your pull request reviewed. Free during beta.
More guides
- How to get software engineering experience before your first job
- What your first code review at a software job looks like
- How to fix a bug in a codebase you didn't write
- What is a software engineering job simulator?
- Money bugs junior Java developers fix in banking code
- Common bugs in a Node.js CMS backend, and how to fix them
- Python ETL bugs junior data engineers fix first
- Payment bugs Python developers fix in a FastAPI service
- Go REST API bugs junior developers fix first
- Booking bugs C# developers fix in ASP.NET Core
- Checkout bugs developers fix in a Next.js store