WebSocket Tester

Runs in browser

Connect to WebSocket servers, send messages, and debug protocols in real-time.

Composer
0 chars

How to Use

Enter a WS/WSS URL and connect.

You can:

  • Send Text or JSON messages
  • View real-time responses
  • Monitor connection states
  • Debug close codes
State
Closed
Content-Type
Text/JSON
Live Console

No messages yet

Connect to a server to see live traffic

About WebSockets: The Complete Guide

WebSockets are a next-generation bidirectional communication technology for web applications which operates over a single socket and is exposed via a JavaScript interface in HTML5 compliant browsers. Unlike traditional HTTP requests, which are isolated and stateless, WebSockets allow for a persistent, full-duplex communication channel between a client and a server.

This persistence makes them ideal for real-time applications where low latency is critical, such as multiplayer games, collaborative editing tools, live stock tickers, and chat applications.

History & Evolution

Before WebSockets (2011), developers used "hacks" to simulate real-time behavior:

  • Polling: Client asks "Any new data?" every X seconds. (Inefficient, high latency)
  • Long Polling: Client asks, server holds connection open until data arrives. (Better, but high server overhead)
  • WebSockets: A single TCP connection is opened and kept alive. Data flows instantly in both directions. No polling required.

Internal Mechanics (Deep Dive)

The WebSocket protocol involves two major phases: the Handshake and the Data Transfer.

1. The Handshake

Every WebSocket connection starts with an HTTP request. The client sends a GET request to the server with special headers:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

If the server accepts the connection, it computes a response key using the `Sec-WebSocket-Key` + a magic string GUID, and replies with 101 Switching Protocols.

2. Data Framing

Once upgraded, communication switches to binary frames. Each frame has a specific structure:

Formatting

Frames contain a header (FIN bit, Opcode, Mask bit, Payload Length) followed by the actual payload.

Masking

Client-to-server frames are constantly masked with a 32-bit key to prevent cache poisoning proxy attacks alongside the path.

Control Opcodes

  • 0x1: Text Frame
  • 0x2: Binary Frame
  • 0x8: Close Connection
  • 0x9: Ping (Heartbeat)
  • 0xA: Pong (Reply)

Implementing WebSockets

WebSockets are supported in almost all modern languages. Here are examples for the most common stacks.

JavaScript (Browser)
const ws = new WebSocket('wss://echo.websocket.org');

ws.onopen = () => {
  console.log('Connected!');
  ws.send('Hello Server');
};

ws.onmessage = (event) => {
  console.log('Received:', event.data);
};
Node.js (ws)
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});
Go (Gorilla)
var upgrader = websocket.Upgrader{} 

func handler(w http.ResponseWriter, r *http.Request) {
    conn, _ := upgrader.Upgrade(w, r, nil)
    defer conn.Close()
    
    for {
        _, msg, _ := conn.ReadMessage()
        conn.WriteMessage(websocket.TextMessage, msg)
    }
}
Python (websockets)
import asyncio
import websockets

async def hello():
    async with websockets.connect("wss://echo.websocket.org") as websocket:
        await websocket.send("Hello world!")
        await websocket.recv()

asyncio.run(hello())

Troubleshooting & Status Codes

When a connection closes, it always provides a code explaining why. Understanding these codes is key to debugging.

Code Meaning Troubleshooting Action
1000 Normal Closure Nothing only. Expected behavior.
1001 Going Away User refreshed the page or server is restarting.
1002 Protocol Error Client/Server sent frame with invalid opcode or payload.
1005 No Status Received Common status when code is missing. Check your network.
1006 Abnormal Closure Most Common Error. Connection dropped without close frame. Check firewall, network, or server crash.
1011 Internal Error Server encountered an unexpected condition. Check server logs.

Firewall & Proxy Issues

If you can connect locally but not in production, check your Load Balancer (Nginx/AWS ALB). WebSockets require:
1. HTTP/1.1 or higher (HTTP/2 supports it natively).
2. Explicitly allowing the `Upgrade` and `Connection` headers to pass through.
3. Increased timeout values (default 60s might be too short for long-lived connections).

Standards

Defined by the IETF as RFC 6455, the WebSocket Protocol is designed to run over TCP (ports 80/443).

Read RFC 6455

API Reference

The W3C WebSocket API defines the JavaScript interface for implementing clients in the browser.

MDN Documentation