Skip to main content

WS Compression

WebSocket Compression

WebSocket compression reduces the size of messages sent over a WebSocket connection, making data transmission more efficient. This is particularly valuable when sending large amounts of repetitive or structured data like JSON payloads, log entries, or real-time updates.

Why it matters: Compression can dramatically reduce bandwidth usage and improve performance, especially for applications that send verbose data formats or operate in bandwidth-constrained environments. We've measured the bandwidth savings of 70-90% for typical JSON WebSocket messages when using compression.

How it works: Compression extensions are negotiated during the WebSocket handshake. If both client and server support the same compression method, messages are automatically compressed before transmission and decompressed upon receipt. Without compression, WebSocket frames are sent as raw, uncompressed data.

This is achieved by supporting the Per-Message Deflate extension, which is the standard for WebSocket compression.

Per-Message Deflate

Per-Message Deflate (permessage-deflate) is the standard WebSocket compression extension, defined in RFC 7692. It uses the DEFLATE algorithm—the same compression method found in gzip and ZIP files—to compress individual WebSocket messages.

tip

We support per-message deflate on all WebSocket subscriptions, including Solana RPC and Chainstream.

WebSocket clients that support per-message deflate

Here's a non-exhaustive list of popular WebSocket libraries that support per-message deflate:

Implementation Examples

import asyncio
import signal
import websockets

from websockets.asyncio.client import connect

async def client():
# NOTE: by default permessage-deflate is enabled
async with connect("wss://api.syndica.io/api-key/<YOUR_API_KEY>") as websocket:
print("CONNECTED")

await websocket.send("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"solana-mainnet.slotSubscribe\"}")

# Close the connection when receiving SIGTERM.
loop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGTERM, loop.create_task, websocket.close())

# Process messages received on the connection.
async for message in websocket:
print("Received message:", message)

asyncio.run(client())