Debugging Real-time Systems — Realtime

Start from the frames (the messages on the WebSocket connection): Chrome DevTools, the Network tab, the Socket filter (formerly WS), the Messages tab (last…

Mr. Darto says his phone never got the news about #12. Eko is sure he pressed the button, and the server logged no error at all. So where did it go wrong?

That is debugging a realtime system: finding out why a piece of news didn't arrive, arrived twice, arrived late or arrived out of order. The hard part is that it involves several devices, a server, the network between them, and time. It works much like tracking a lost parcel with a courier. You don't guess. You open the record at every handover point (sent, received at the depot, out with the courier, delivered), and every parcel has a tracking number so the records can be matched. In a realtime system, the parcel is one piece of news, the handover points are the tablet, the server and the phone, and the tracking number is a number the news carries (cid and seq below).

Look at the frames in DevTools

In Chrome:

  1. Open DevTools (the browser's built-in inspection tools), then the Network tab.
  2. Pick the Socket filter. In older versions and some docs it is called WS. It now covers WebSocket, WebTransport and DirectSocket.
  3. Click the connection, then open the Messages tab.

The Messages tab holds the last 100 messages: sent ones light green, received ones white, and you can filter them with a regex (a search pattern). Tick Preserve log so the list stays after a reload.

These are real frames from Chromium 147 (the base of Chrome). The board page connected, then Eko sent a status with an ack. The frames were read through the DevTools Protocol, where the Messages tab gets its data:

sent      2probe                                                  6 B
received  3probe                                                  6 B
sent      5                                                       1 B
sent      420["order:status",{"id":12,"status":"Ready for pickup","cid":"c-7f3a"}]   72 B
received  42["order:status",{"id":12,"status":"Ready for pickup","seq":4}]           64 B
received  430[{"ok":true}]                                       16 B

In Firefox: the Network tab, the WS filter, click the connection, then the Response tab. Firefox also shows Socket.IO message content (the payload) as a tree (a list with levels). For SSE, Chrome has an EventStream tab, and Firefox shows it in the Response tab. Throttling (a setting that slows the network) in the Network tab slows WebSockets down too, handy for imitating a phone on a weak signal.

Turn on Socket.IO's logs

On the server, turn logs on with the DEBUG variable when you start Node. It holds the names of the log parts you want to see:

$ DEBUG=socket.io:socket,socket.io:client node server.js
2026-10-11T03:51:06.114Z socket.io:socket join room order:12
2026-10-11T03:51:06.217Z socket.io:socket got packet {"type":2,"nsp":"/","id":0,"data":["order:status",{"id":12,"status":"Ready for pickup","cid":"c-7f3a"}]}
2026-10-11T03:51:06.217Z socket.io:socket sending ack [{"ok":true}]
2026-10-11T03:51:07.616Z socket.io:client client close with reason transport close

The line join room order:12 appears every time a socket joins that room. DEBUG=socket.io* turns on all log parts, Engine.IO included. The date and time at the start of each line (ISO format) only appear when the output goes to a file. In a normal terminal you see a time difference such as +0ms at the end of the line instead.

In the browser, run localStorage.debug = 'socket.io-client:socket' in the console, then reload. Tested: this only works with a build (a ready-to-use library file) that carries the debug module, that is, /socket.io/socket.io.js served by the server, or import { io } from 'socket.io-client/debug'. If you use a bundler (a tool that joins JavaScript files, such as Vite) with import { io } from 'socket.io-client', the build it picks is build/esm. That build does not carry debug, so nothing prints. socket.io.min.js doesn't carry it either.

Logs with a tracking number: cid and seq

Socket.IO's log shows packets, but it doesn't show your app's flow. Write one structured log line (one JSON per line) at every handover point. Each line carries the same numbers from end to end: cid from the Chat lesson (a number the sender makes for each message) and seq from the Dashboard lesson (a sequence number the server raises).

const log = (hop, data) => console.log(JSON.stringify({ t: new Date().toISOString(), hop, ...data }))

socket.onAny((event, ...args) => log('server.received', { socket: socket.id, event, cid: args[0]?.cid }))
socket.onAnyOutgoing((event, ...args) => log('server.sent', { socket: socket.id, event, seq: args[0]?.seq }))

// when broadcasting, record how many sockets are in the room too
const members = io.sockets.adapter.rooms.get('order:12')?.size ?? 0
log('server.emit', { room: 'order:12', cid, seq, sockets: members })

socket.onAny runs for every event the socket receives, and socket.onAnyOutgoing for every event it sends. hop is the name of the handover point.

Then search one cid across everyone's logs. For example, the tablet logged that it sent the news, the server logged that it received it and broadcast it to 1 socket, and Mr. Darto's phone logged nothing. Then the problem sits between the server and the phone, not in the tablet.

Force the race so it repeats

A timing bug, such as a race (two events compete, and the result depends on which comes first), often appears only once in ten tries. Add an artificial delay at one point so the order is always the same, then test the fix with that delay:

// temporary, dev only: make the snapshot always 300 ms slow
const snap = await new Promise((r) => setTimeout(r, 300)).then(() => fetch('/api/dashboard'))

With this delay, the race between the snapshot and the delta from the Dashboard lesson happens every time, not only sometimes. Only believe the fix once it passes ten times in a row with each delay: 0, 300 and 1,000 ms.

The failures we have met so far

Before you look at the table, guess: the laundry Wi-Fi drops for a second. Mr. Darto's phone reconnects, but from then on it never receives news about #12. In DevTools' Messages tab, frames 2 and 3 still appear every 25 seconds, but there is no 42["order:status",...]. What is the most likely cause?

Symptom Cause Where to look
The seventh tab hangs with no error SSE over HTTP/1.1: 6 connections per domain Network: the seventh request is pending. Use HTTP/2
Clients on process B get no news No Redis adapter, or B was restarting (Pub/Sub keeps nothing) The PUBLISH reply: how many received it
disconnect only comes 20 to 45 seconds after the phone suddenly dies Ping timeout: 25 seconds interval + 20 seconds timeout The reason ping timeout in the server log
News processed twice A resend (retry) without a cid check Two log lines with the same cid
No news after a reconnect The room was joined only once; the new socket is not in it DEBUG log: join room order:12 doesn't appear again
An event never handled, with no error Event names differ: emit order:status, listen order-status The frame is in Messages, its handler never runs
Another site's page connects as Mrs. Rini CSWSH (Cross-Site WebSocket Hijacking): Origin not checked The Origin header in the handshake
A quiet connection drops at second 60 nginx's proxy_read_timeout Code 1006 in the close event

For Mr. Darto's phone, the cause is a stale room after the reconnect. The connection is alive (ping and pong still appear). But after the reconnect, the server made a new socket for that phone, and the new socket never ran join for order:12. In the server log, the line join room order:12 doesn't appear after the reconnect. The fix: send follow (the event that asks to join a room) in the connect handler, which runs every time the phone connects. When messages don't reach one client, check two things first: whether the socket joined the right room, and whether the event name is exactly the same on the server and the client.

Numbers to watch

⚠️ Habits that make realtime bugs hard to find

TL;DR: Start from the frames (the messages on the WebSocket connection): Chrome DevTools, the Network tab, the Socket filter (formerly WS), the Messages tab (last 100 messages, a regex filter, and Preserve log to keep them across a reload). In Firefox, the WS filter and the Response tab. SSE is in the EventStream tab. Ping 2 and pong 3 every 25 seconds mean a healthy Socket.IO connection. Turn on server logs with DEBUG=socket.io*. In the browser, localStorage.debug only works with /socket.io/socket.io.js or socket.io-client/debug. Record every handover point (tablet, server, phone) with cid (a number from the sender) and seq (a sequence number from the server). Force races (events whose order sometimes swaps) with an artificial delay. Match symptoms against the list: the seventh SSE tab, the Redis adapter, the 45 second ping timeout, duplicate deliveries, a stale room after reconnect (the new socket is not in the room yet), mismatched event names, CSWSH (another site opens a WebSocket with the user's cookie), the 60 second proxy. Watch the connection count, the reconnect rate per reason, and message lag.

⚡ Start from the frames

Meaning: Frames are the messages that really go over a WebSocket connection. In Chrome: open DevTools, the Network tab, the Socket filter, click the connection, then the Messages tab. In Socket.IO, the server sends ping 2 every 25 seconds and the client replies pong 3: that means the connection is healthy. If those are the only frames, no news is being sent to this connection.

⚡ Logs with a tracking number

Write:

const log = (hop, data) => console.log(JSON.stringify({ t: new Date().toISOString(), hop, ...data }))

socket.onAny((event, ...args) => log('server.received', { socket: socket.id, event, cid: args[0]?.cid }))

Meaning: Like a courier recording the tracking number at every handover point, write one log line at every point the news passes: tablet, server, phone. Each line carries the same numbers from end to end: cid (a number the sender makes for each message) and seq (a sequence number from the server). In this code, socket.onAny records every event the server receives (the point 'server.received'). Then search one cid across everyone's logs to find where the news stopped.

⚡ A stale room after the reconnect

Guess first: Mr. Darto's phone gets the news about order #12 through the room order:12. The laundry Wi-Fi drops for a second. The phone reconnects, but from then on it never receives news about #12, though ping 2 and pong 3 still appear. What is the most likely cause?

A stale room after the reconnect. The connection is alive, but after the reconnect the server made a new socket, and that new socket never joined order:12. The fix: send follow (the request to join the room) in the connect handler, which runs every time the phone connects.

Meaning: When messages don't reach one client, check two things first: whether the socket joined the right room, and whether the event name is exactly the same on the server and the client.

What you will learn