WebSockets¶
curl_cffi provides high-performance WebSocket clients for synchronous and asynchronous contexts.
Both clients are powered by a heavily optimized, SIMD-accelerated libcurl build capable of multi-gigabit throughput. The synchronous client provides a simple, direct blocking interface alongside an optional event-driven callback loop, while the asynchronous client utilizes an efficient non-blocking I/O architecture.
Quick Start¶
The recommended way to connect to a WebSocket is via the Session or AsyncSession context managers.
Sync client:
from curl_cffi import Session
with Session() as session:
with session.ws_connect("wss://ws.postman-echo.com/raw") as ws:
ws.send_str("Hello, World!")
print(ws.recv_str()) # Hello, World!
Async client:
import asyncio
from curl_cffi import AsyncSession
async def main():
async with AsyncSession() as session:
async with session.ws_connect("wss://ws.postman-echo.com/raw") as ws:
await ws.send_str("Hello, World!")
print(await ws.recv_str()) # Hello, World!
Compression¶
Added in version 0.17.0b1.
Enable permessage-deflate with CurlOpt.WS_OPTIONS:
from curl_cffi import CurlOpt, CurlWsOption, Session
with Session() as session:
with session.ws_connect(
"wss://example.com/ws",
curl_options={CurlOpt.WS_OPTIONS: CurlWsOption.PERMESSAGE_DEFLATE},
) as ws:
ws.send_str("Hello, World!")
print(ws.recv_str())
The same curl_options argument works with AsyncSession.ws_connect() and
WebSocket.connect(). Compression is opt-in and requires a zlib-enabled
curl-impersonate 2.3.0 or newer.
Libcurl adds the extension offer
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits and
handles compression and decompression after the server accepts it. If the server
declines, messages remain uncompressed. Adding the header manually does not
enable compression. HTTP accept_encoding is a separate setting.
Send and receive methods continue to use uncompressed application data, and
max_message_size limits the decompressed message size. Low-level frame
metadata may report bytesleft == -1 while the decoded size is unknown;
bytesleft == 0 marks the end of a frame.
CurlWsOption values are options for CurlOpt.WS_OPTIONS, separate from the
CurlWsFlag values passed to send(). Options can be combined with |,
but PERMESSAGE_DEFLATE cannot be combined with RAW_MODE.
Client Selection¶
Both clients send and receive the same messages and share the same timeouts and error handling. The difference is how they perform I/O.
Synchronous |
Asynchronous |
|
|---|---|---|
Class |
|
|
Opened with |
|
|
How I/O works |
Each call blocks until it finishes or times out |
Background tasks read and write through queues |
Send while receiving |
No, one thread at a time |
Yes |
Event callbacks |
Yes, with |
No |
Good fit for |
Scripts and simple request/reply exchanges |
Long-lived streams that send and receive at once |
Tip
If your application already uses asyncio, use the asynchronous client. It’s also the right choice whenever you need to send and receive at the same time.
Synchronous Client¶
The synchronous WebSocket provides a traditional blocking interface. Method calls like recv() and send() will block the current thread until the network operation completes or times out.
Connecting¶
Use ws_connect from a Session. This method accepts the same network parameters as standard HTTP requests, including impersonation, proxies, and cookies.
Session proxy and certificate verification settings are inherited unless
overridden on the connection. For example, verify="/path/to/ca.pem" on the
session supplies the CA bundle for its WebSocket handshakes as well.
with Session() as session:
# Session cookies are automatically injected into the WebSocket handshake
session.cookies.set("session_id", "xyz")
# Context manager (recommended)
with session.ws_connect(
"wss://api.example.com/v1/stream",
impersonate="chrome",
proxies={"all": "socks5h://localhost:9050"},
timeout=10 # Connection phase timeout
) as ws:
pass
# Manual lifecycle management
ws = session.ws_connect("wss://api.example.com")
ws.send_str("Hello")
ws.close() # Explicit Close is required
Sending & Receiving¶
All sending and receiving methods are blocking. Sending methods return the number of bytes successfully written to the socket.
# Send data
ws.send_str("Hello", timeout=5.0)
ws.send_bytes(b"\x00\x01\x02")
ws.send_json({"action": "subscribe"}, timeout=5.0)
# Receive data (decodes utf-8 automatically)
msg = ws.recv_str(timeout=5.0)
# Receive parsed JSON
data = ws.recv_json()
recv() gives you the message exactly as it arrived, with no UTF-8 validation. Use recv_str() or recv_json() if you need that check.
Event Callbacks & run_forever()¶
For applications that prefer an event-driven approach, the synchronous client supports callbacks and a blocking run_forever() loop.
def on_message(ws: WebSocket, message: bytes | str):
print(f"Received: {message}")
def on_error(ws: WebSocket, error: Exception):
# CurlError for transport errors, Exception for callbacks.
print(f"Error: {error}")
def on_open(ws: WebSocket):
print("Connection open")
def on_close(ws: WebSocket, close_code: int, close_reason: str):
print(f"Connection closed: {close_reason}")
with Session() as session:
with session.ws_connect(
"wss://echo.websocket.org",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
) as ws:
ws.send_str("Hello World!")
# Blocks the thread and dispatches events as they arrive
ws.run_forever()
These callbacks are only dispatched by run_forever() — they don’t fire for direct send() / recv() use.
Thread Safety¶
A synchronous WebSocket is not thread-safe. It wraps a single libcurl easy handle, and calling send() in one thread while another is blocked in recv() can corrupt libcurl’s internal state, leading to undefined behaviour or a segmentation fault.
To send and receive at the same time, use the asynchronous client.
Asynchronous Client¶
The AsyncWebSocket client uses a highly optimized non-blocking I/O Architecture:
Outgoing: Messages are queued for immediate delivery. A background task handles the actual network transmission.
Incoming: A background task continuously reads from the network and populates a receive queue, separating your application logic from network speeds.
Connecting¶
Use the ws_connect context manager from an AsyncSession.
async with AsyncSession() as session:
async with session.ws_connect("wss://api.example.com/v1/stream") as ws:
...
These connection styles are also supported:
# Manual connection management
ws = await session.ws_connect("wss://api.example.com")
await ws.send_str("Hello")
await ws.close() # Explicit Close is required
# Same as context manager, alternate style
async with await session.ws_connect("wss://api.example.com") as ws:
await ws.send_str("Hello")
Sending Data¶
Because sending is queued, these methods return immediately unless the internal send queue is full (backpressure).
Async send methods return None rather than the sent byte-count.
# Queues data for immediate delivery
await ws.send_str("Hello")
await ws.send_json({"action": "subscribe"})
Flushing:
If the application logic requires confirmation that messages have been successfully handed off to the underlying network socket, use flush().
await ws.send_str("Critical Data")
await ws.flush() # Waits until all queued messages are transmitted
Receiving Data¶
Concurrent calls to receive methods are fully supported. Messages are distributed to waiters in FIFO order.
# Receive as string
msg = await ws.recv_str(timeout=5.0)
# Iteration (yields raw bytes)
async for message in ws:
print(message.decode("utf-8"))
# Receive raw binary and flags
payload, flags = await ws.recv(timeout=5.0) # Omit 'await' in Sync
if flags & CurlWsFlag.BINARY:
print(f"Received binary data: {payload}")
recv() gives you the payload exactly as it arrived, with no UTF-8 validation. Use recv_str() or recv_json() if you need that check.
Async-Only Advanced Configuration¶
The AsyncWebSocket client exposes several advanced configuration options to tune its I/O architecture.
Queue Sizes (Backpressure)¶
You can control the internal buffer sizes to manage TCP backpressure. These values also influence the maximum possible memory footprint.
recv_queue_size (default: 64): Max incoming messages to buffer internally.
max_message_sizecaps how large each one can be, so the worst case is fixed atrecv_queue_size×max_message_size— 256MB with the defaults.send_queue_size (default: 32): Max outgoing messages to buffer before
send()blocks. Outgoing messages have no size limit, so size this against your own largest message.block_on_recv_queue_full (default:
True): Behavior when the receive queue is full. IfTrue, the reader blocks until there is space in the queue (may cause timeouts). IfFalse, the connection fails immediately to prevent data loss.drain_on_error (default:
False): When a fatal error occurs, normally it is raised immediately. When this option is enabled, calls torecv()will yield all buffered messages first before raising the exception.
Queue size interacts with cache residency: the working set is queue_size × typical message size, and once it exceeds the CPU’s L2/L3 cache, throughput drops.
# Many small messages (< 4KB): larger queues cut scheduling overhead
ws = await session.ws_connect(url, recv_queue_size=1024, send_queue_size=1024)
# Streaming large messages (>= 64KB): smaller queues keep the working
# set in cache and measurably improve throughput
ws = await session.ws_connect(url, recv_queue_size=16, send_queue_size=16)
Frame Coalescing¶
This is an optional power-user optimization technique which breaks frame boundaries and concatenates multiple pending messages from the send queue into a single WebSocket frame. This significantly reduces system call overhead and boosts throughput for chatty streams with small payloads.
Warning
Multiple messages will arrive as a single merged payload. Ensure your server application can handle concatenated strings/bytes.
Large batches share the outgoing queue with automatic PONG replies and can delay them. Lower max_send_batch_size if your server has a strict ping timeout.
coalesce_frames (default:
False): Enable frame coalescing.max_send_batch_size (default: 64): Max messages to merge per frame.
>>> import asyncio
>>> from curl_cffi import AsyncSession, Response
>>> async def test_coalescing():
... """Test frame coalescing feature"""
... async with AsyncSession[Response]() as session:
... async with session.ws_connect("wss://ws.postman-echo.com/raw", coalesce_frames=True) as ws:
... # Take advantage of concurrent sends in quick succession
... await asyncio.gather(
... ws.send_str("Concurrent sending"),
... ws.send_str(" is "),
... ws.send_str("so cool!!"),
... )
... response: str = await ws.recv_str()
... print(response)
...
>>> asyncio.run(test_coalescing())
Concurrent sending is so cool!!
Cooperative Multitasking¶
To adjust event loop fairness during high-volume streams, you can tune the time-based cooperative scheduler:
recv_time_slice (default: 0.01s): Max time spent processing incoming messages before yielding (10ms).
send_time_slice (default: 0.01s): Max time spent sending messages before yielding (10ms).
# Force more frequent yields for lower latency in other tasks (1ms)
ws = await session.ws_connect(url, recv_time_slice=0.001)
Upgrading from Earlier Versions¶
Auto-Reassembly & recv_fragment:
The WebSocket clients now handle message fragmentation and reassembly automatically. You are guaranteed to receive complete logical messages when calling recv(), recv_str(), or recv_json().
Synchronous WebSocket.recv_fragment() remains available for code that needs raw fragments. It does not wait for socket readiness and can raise CurlError with CurlECode.AGAIN. Do not mix it with recv() while a partial message is buffered.
AsyncWebSocket.recv_fragment() remains unsupported. Use recv() for complete messages; its background reader handles reassembly. Do not call Curl.ws_recv() on an AsyncWebSocket managed connection, since that would compete with the background reader. Applications requiring custom fragment handling can manage a low-level Curl connection themselves.
When migrating a custom fragment loop to recv(timeout=...), note that its deadline still applies after the first fragment arrives; partial messages are retained after a timeout.
Closing a session closes its WebSockets:
Closing a session, or leaving its context manager block, now closes any WebSocket still open on it with a 1001 (going away) frame. Keep the session open for as long as the connection is needed.
WebSockets count against max_clients:
Each async WebSocket holds one of the AsyncSession curl handles for its lifetime. If every handle is held by a WebSocket, further requests raise RequestException rather than waiting indefinitely. Increase max_clients if you need more connections at once.
send() timeout:
Pass timeout= by name. AsyncWebSocket.send(payload, flags, timeout) remains supported for compatibility; the new synchronous send timeout is keyword-only.
Performance Tuning¶
The WebSocket protocol requires every client-to-server message to be masked (XOR) according to RFC 6455.
Curl-CFFI uses a customized build of libcurl enhanced with AVX-512/AVX2/NEON SIMD vectorized masking. It is capable of multi-gigabit throughput in both directions.
If your application needs to send large volumes of data, you should focus on sending fewer, larger messages (e.g., 64KB to 1MB per message). This minimizes the framing and FFI overhead, allowing the C-layer to process the payload at hardware limits.