Reliable connections
Warning
The rews (reliable WebSocket) package wraps a standard WebSocket connection and adds automatic reconnection when the connection is lost. On reconnect, it restores the previous session state including namespace, database, authentication, variables, and live queries.
This is useful for long-running applications that need to survive transient network failures without manual reconnection logic.
API References
| Function | Description |
|---|---|
rews.New(newConn, interval, unmarshaler, logger) | Creates a new auto-reconnecting WebSocket connection |
rews.NewExponentialBackoffRetryer() | Creates a retryer with exponential backoff and jitter |
rews.NewFixedDelayRetryer(delay, maxRetries) | Creates a retryer with a fixed delay between attempts |
Setting up a reliable connection
Create a rews.Connection by providing a factory function that constructs the underlying WebSocket connection, a check interval for detecting disconnections, a CBOR unmarshaler, and an optional logger.
Then pass the connection to FromConnection to create a *DB:
Configuring retry behavior
By default, connection attempts are not retried. Set the Retryer field to enable automatic retries on connection failure.
The ExponentialBackoffRetryer increases the delay between retries exponentially, with optional jitter to avoid thundering herd problems:
| Field | Default | Description |
|---|---|---|
InitialDelay | 1s | Delay before the first retry |
MaxDelay | 30s | Maximum delay between retries |
Multiplier | 2.0 | Exponential backoff multiplier |
MaxRetries | 0 (infinite) | Maximum retry attempts, 0 for unlimited |
Jitter | true | Add randomness to avoid synchronized retries |
JitterFactor | 0.3 | Maximum jitter as a fraction of the delay |
For simpler cases, FixedDelayRetryer uses a constant delay:
You can also implement the Retryer interface for custom strategies:
What gets restored on reconnect
When the connection is lost and re-established, rews automatically restores:
Namespace and database -- the last values passed to
.Use()Authentication -- the last token from
.SignIn(),.SignUp(), or.Authenticate()Live queries -- all active live queries are re-subscribed, and notification routing is restored
Note
Connection states
The rews.Connection tracks its state through a state machine:
| State | Description |
|---|---|
Disconnected | Not connected, initial state |
Connecting | Connection attempt in progress |
Connected | Connection established and active |
Closing | Close requested, shutting down |
Closed | Fully closed, cannot be reused |
Learn more
Connecting to SurrealDB for standard connection setup
Live queries for live query setup that persists across reconnections
DB API reference for
FromConnectionusage