# Debugging with DevTools, curl and websocat — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-debugging

> Inspect SSE and WebSocket traffic with browser DevTools and command-line tools.

## Seeing what is on the wire

Real-time bugs are usually about *what was sent when*, so start by looking at the traffic. In Chrome or Edge DevTools, open the **Network** panel and filter to WebSocket traffic (the filter button reads **WS** or **Socket** depending on the version; Firefox has a similar one). Click the connection: the **Headers** tab shows the upgrade request and the 101 response, including `Origin`, subprotocols and cookies, and the **Messages** tab lists every frame with direction, size and timestamp. For SSE, select the request and open the **EventStream** tab to see each event's id, type and data as it arrives. If a stream shows nothing, check the response's `Content-Type` and whether the server or a proxy is buffering. From a terminal, `curl -N` prints SSE events as they arrive, and **websocat** or **wscat** give an interactive WebSocket prompt, ideal for testing a server without writing a client. When frames look right but the UI is wrong, the bug is in your client code, not the network.

## Three levels of checking

Start where the user is (the browser), move to the command line to rule out the front end, and lock the behaviour in with automated tests.

![Three connected stages in order: browser DevTools inspection, command-line tools such as curl and websocat, automated tests.](assets/figures/realtime-web/section-9-map.svg) — Figure 9.1: browser DevTools -> curl and websocat -> automated tests

## Command-line tools

Each tool talks to the endpoint directly, bypassing your front-end code. `websocat` is a separate binary; `wscat` runs through `npx`.

```bash
# Watch an SSE stream live (-N turns off curl's output buffering)
curl -N -H "Accept: text/event-stream" https://api.example.in/events

# Open an interactive WebSocket session (type a line, press Enter to send)
websocat wss://api.example.in/ws

# Same idea with the Node-based wscat tool
npx wscat -c wss://api.example.in/ws
```

## What curl -N shows for an SSE endpoint

Run against a local SSE server, curl prints the comment line and then the event exactly as they appear on the wire.

```bash
curl -N http://localhost:8096/events
```

Output:

```
: connected

id: 1
data: hello
```

## Log close codes and reasons

Add the close code, reason and connection age to your client and server logs. A pattern such as "1006 after exactly 60 s" or "1009 on large uploads" turns a vague "sockets keep dropping" ticket into a one-line fix.

**Quiz:** Where in Chrome DevTools do you see the individual messages of an open WebSocket?

- [x] Network panel, filter to WebSocket traffic, select the connection, Messages tab
- [ ] Application panel, Cookies section
- [ ] Console panel, by typing `ws.messages`
- [ ] Sources panel, under Event Listener Breakpoints only

*Answer:* Network panel, filter to WebSocket traffic, select the connection, Messages tab. The Network panel lists the WebSocket request; its Messages tab shows each frame sent and received with timestamps.
