How to test a WebSocket API
Realtime features, like chat, live dashboards, notifications and collaborative editing, often run over WebSockets. They're awkward to test with tools built for request and response: a WebSocket is a connection that stays open, where both sides can send messages at any time.
Requestarium has a dedicated WebSocket request type for this. Here's how to use it.
The parts of a WebSocket request
| Tab | What goes there |
|---|---|
| Connection | The ws:// or wss:// URL, subprotocols, timeout, reconnect behavior and message type |
| Authorization | Auth for the handshake, inherited from the collection like other requests |
| Headers | Extra handshake headers |
| Messages | Text or JSON messages to send once connected |
| Events | The message lifecycle and how received messages are handled |
| Scripts | Before connect and On message |
Authenticating the handshake
A WebSocket starts as an HTTP request (the handshake) and is upgraded to a socket. That's the only moment HTTP headers are sent, so it's where auth happens. Depending on the server:
- A header:
Authorization: Bearer {{token}}, set on the collection and inherited. - A query parameter:
wss://realtime.example.com/socket?token={{token}}, common because browsers can't set headers on WebSocket connections. - A first message: some servers expect an
authmessage right after connecting. Put it first in Messages.
Sending messages
Add the messages to send after connecting, as text or JSON:
{ "type": "subscribe", "channel": "orders", "accountId": "{{accountId}}" }Variables work in messages, so the same request subscribes to different accounts in different environments.
Checking what comes back
The On message script handles incoming messages. Use it the way you'd use a post-response script on an HTTP request: check the shape of events with pm.test, and store values you need later, such as an ID from a created event, with pm.environment.set. console.log output appears in the diagnostics drawer, which is often the quickest way to see what the server actually sends.
The Before connect script can prepare the URL, headers and variables, for example to sign the connection URL.
Subprotocols
Servers like GraphQL subscriptions over WebSocket, or STOMP, negotiate a subprotocol in the handshake. Set it in Connection (for example graphql-transport-ws), or the server will close the connection immediately.
Debugging connections
- The connection closes at once: usually auth, a missing subprotocol, or an origin check. Look at the close code:
1008is a policy violation,1002a protocol error. - Connects but nothing arrives: you may need to send a subscribe message first.
- Works locally, not on staging: a proxy or load balancer between you and the server may not support WebSockets, or may time out idle connections. Many servers expect a ping or heartbeat.
The diagnostics drawer shows console output from your scripts and the connection's events.