Configuration¶
JetSocket uses dataclasses for type-safe configuration.
BackoffConfig¶
Controls reconnection backoff behavior.
Configuration for exponential backoff.
Attributes:
| Name | Type | Description |
|---|---|---|
base |
float
|
Initial delay in seconds. |
multiplier |
float
|
Exponential multiplier applied each attempt. |
cap |
float
|
Maximum delay in seconds. |
jitter |
bool
|
Whether to add random jitter (recommended). |
max_attempts |
int
|
Maximum number of retry attempts (0 = infinite). |
reset_after |
float
|
Reset attempt counter after this many seconds connected. |
Source code in src/jetsocket/backoff.py
__post_init__ ¶
Validate configuration values.
Source code in src/jetsocket/backoff.py
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
base |
float | 1.0 | Initial delay in seconds |
multiplier |
float | 2.0 | Delay multiplier per attempt |
cap |
float | 60.0 | Maximum delay in seconds |
jitter |
bool | True | Add random jitter to prevent thundering herd |
max_attempts |
int | 0 | Maximum attempts (0 = infinite) |
reset_after |
float | 60.0 | Reset backoff after N seconds of successful connection |
Example¶
from jetsocket import BackoffConfig
# Aggressive reconnect for trading
backoff = BackoffConfig(
base=0.5,
multiplier=2.0,
cap=30.0,
jitter=True,
max_attempts=0, # Never give up
)
# Quick fail for user-facing apps
backoff = BackoffConfig(
base=1.0,
multiplier=2.0,
cap=10.0,
max_attempts=5, # Give up after 5 attempts
)
HeartbeatConfig¶
Controls ping/pong heartbeat behavior.
Configuration for heartbeat behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Seconds between ping frames. |
30.0
|
timeout
|
float
|
Seconds to wait for pong before considering connection dead. |
10.0
|
payload
|
bytes
|
Custom payload for ping frames. |
b''
|
use_ws_ping
|
bool
|
If True, use WebSocket-level PING frames (RFC 6455). |
True
|
application_ping
|
Callable[[], bytes | str | dict[str, Any]] | None
|
Custom function to generate application-level ping. |
None
|
pong_matcher
|
Callable[[Any], bool] | None
|
Custom function to detect application-level pong responses. |
None
|
Source code in src/jetsocket/heartbeat.py
__post_init__ ¶
Validate configuration.
Source code in src/jetsocket/heartbeat.py
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
interval |
float | 30.0 | Send ping every N seconds |
timeout |
float | 10.0 | Wait N seconds for pong |
payload |
bytes | None | Custom ping payload |
use_ws_ping |
bool | True | Use WebSocket ping frames |
use_application_ping |
bool | False | Send as application message |
pong_matcher |
Callable | None | Function to detect pong in messages |
Example¶
from jetsocket import HeartbeatConfig
# Standard WebSocket ping
heartbeat = HeartbeatConfig(interval=20.0, timeout=10.0)
# Application-level ping (e.g., Bybit)
heartbeat = HeartbeatConfig(
interval=20.0,
timeout=10.0,
payload=b'{"op": "ping"}',
use_ws_ping=False,
use_application_ping=True,
pong_matcher=lambda msg: msg.get("op") == "pong",
)
BufferConfig¶
Controls message buffering behavior.
Configuration for message buffering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
Maximum number of messages to buffer. |
1000
|
overflow_policy
|
OverflowPolicyType
|
Policy when buffer is full. - 'drop_oldest': Remove oldest message (FIFO eviction). - 'drop_newest': Discard incoming message. - 'error': Raise BufferOverflowError. |
'drop_oldest'
|
enable_dedup
|
bool
|
Whether to deduplicate messages by sequence ID. |
False
|
dedup_window
|
int
|
Number of recent sequence IDs to track for deduplication. |
100
|
Source code in src/jetsocket/buffer.py
__post_init__ ¶
Validate configuration.
Source code in src/jetsocket/buffer.py
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
capacity |
int | 1000 | Maximum messages to buffer |
overflow_policy |
str | "drop_oldest" | Policy when full: "drop_oldest", "drop_newest", "error" |
enable_dedup |
bool | False | Enable deduplication |
dedup_window |
int | 100 | Track last N sequence IDs |
Example¶
from jetsocket import BufferConfig
# Trading: large buffer with dedup
buffer = BufferConfig(
capacity=10000,
overflow_policy="drop_oldest",
enable_dedup=True,
dedup_window=1000,
)
# Dashboard: small buffer, drop stale
buffer = BufferConfig(
capacity=100,
overflow_policy="drop_oldest",
)
ReplayConfig¶
Controls replay-on-reconnect behavior.
Configuration for message replay on reconnection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ReplayModeType
|
Replay strategy. - 'none': No replay. - 'sequence_id': Call on_replay callback with last sequence ID. - 'full_buffer': Resend all buffered messages. |
'none'
|
sequence_extractor
|
Callable[[Any], SequenceId] | None
|
Function to extract sequence ID from a message. |
None
|
on_replay
|
Callable[[SequenceId], Awaitable[None]] | None
|
Async callback invoked with last sequence ID (for sequence_id mode). |
None
|
Source code in src/jetsocket/buffer.py
__post_init__ ¶
Validate configuration.
Source code in src/jetsocket/buffer.py
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
mode |
str | "none" | Replay mode: "none", "sequence_id", "full_buffer" |
sequence_extractor |
Callable | None | Function to extract sequence ID from message |
on_replay |
Callable | None | Callback when replay is needed |
Example¶
from jetsocket import ReplayConfig
async def request_replay(last_seq):
await ws.send({"action": "replay", "from": last_seq})
replay = ReplayConfig(
mode="sequence_id",
sequence_extractor=lambda msg: msg.get("seq"),
on_replay=request_replay,
)
ConnectionPoolConfig¶
See ConnectionPool API reference for connection pool configuration.