Skip to main content

RFC 6455 - The WebSocket Protocol

Published: December 2011
Status: Standards Track
Authors: I. Fette (Google Inc.), A. Melnikov (Isode Ltd.)


Abstract

The WebSocket Protocol enables full-duplex communication between a client and a server over a single TCP connection. The WebSocket Protocol is designed to be implemented in web browsers and web servers, but it can be used by any client or server application.

The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.


Table of Contents


Additional Resources


  • RFC 6455: The WebSocket Protocol ← This document
  • RFC 7692: WebSocket Compression Extensions
  • RFC 8441: Bootstrapping WebSockets with HTTP/2

Quick Reference

WebSocket URI Format

ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
  • ws:// - Unencrypted connection (default port 80)
  • wss:// - TLS encrypted connection (default port 443, recommended for production)

Handshake Example

Client Request:

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

Server Response:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Frame Types (Opcode)

OpcodeTypeDescription
0x0ContinuationContinuation frame
0x1TextText frame (UTF-8)
0x2BinaryBinary frame
0x8CloseClose frame
0x9PingPing frame (heartbeat)
0xAPongPong frame (heartbeat response)

Close Status Codes

CodeNameDescription
1000Normal ClosureNormal closure
1001Going AwayEndpoint going away (e.g., page navigation)
1002Protocol ErrorProtocol error
1003Unsupported DataUnsupported data type
1006Abnormal ClosureAbnormal closure (no Close frame sent)
1009Message Too BigMessage too large
1011Internal ErrorServer internal error

Frame Structure (Basic Format)

 0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+-------------------------------+
| Extended payload length continued, if payload len == 127 |
+-------------------------------+-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------+-------------------------------+

Key Points:

  • FIN: 1=final frame, 0=more frames follow
  • MASK: Must be 1 for client→server, must be 0 for server→client
  • Opcode: Frame type identifier
  • Payload Length: Data length (0-125 direct, 126 uses 16-bit, 127 uses 64-bit)

Core Terminology

English TermDescription
Full-Duplex CommunicationBidirectional simultaneous communication
Opening HandshakeHTTP upgrade process to WebSocket
Closing HandshakeGraceful connection closure process
FrameBasic unit of data transmission
MessageComposed of one or more frames
MaskingClient data must be masked (security mechanism)
FragmentationLarge messages transmitted in multiple frames