Protocol (v1)
This documents the old v1 API. See Protocol for the current version.
Zocket uses a simple, JSON-based protocol over standard WebSockets.
Transport & Serialization
Section titled “Transport & Serialization”- Transport: Standard WebSockets (
ws://orwss://). - Serialization: Every message is a JSON-encoded object.
Connection Lifecycle
Section titled “Connection Lifecycle”Handshake & Authentication
Section titled “Handshake & Authentication”Because the browser’s native WebSocket API does not support custom HTTP headers, Zocket uses URL Query Parameters for initial authentication.
- The client connects to the server URL
- Headers defined in
zocket.create({ headers: ... })are passed as query parameters - The server validates the headers against the schema before completing the WebSocket upgrade
Client ID
Section titled “Client ID”Upon successful connection, the server assigns a unique clientId.
Protocol Versioning
Section titled “Protocol Versioning”@zocket/client automatically appends its version via the x-zocket-version query parameter.
Message Formats
Section titled “Message Formats”Standard Message
Section titled “Standard Message”{ "type": "chat.message", "payload": { "text": "Hello!", "from": "user_1" }}RPC (Request/Response)
Section titled “RPC (Request/Response)”Request (Client → Server):
{ "type": "users.getProfile", "payload": { "id": "123" }, "rpcId": "unique-msg-id-456"}Response (Server → Client):
{ "type": "__rpc_res", "payload": { "name": "John" }, "rpcId": "unique-msg-id-456"}Internal Mechanisms
Section titled “Internal Mechanisms”Efficient Broadcasting
Section titled “Efficient Broadcasting”Zocket uses a reserved internal topic __zocket_all__ for global broadcast using the native adapter’s publish method.
Rooms are implemented using the underlying WebSocket engine’s pub/sub system (ws.subscribe(roomId), ws.unsubscribe(roomId)).