Subscription Methods
ExchangeWebSocketClient manages the market WebSocket connection and subscribes to real-time order, trade, cancel, and resolution updates by assetId.
import {
createExchangeWebSocketClient,
} from "@gammaswap/v2-exchange-sdk";
const marketStream = createExchangeWebSocketClient({
websocketUrl: "wss://exchange-api.gammaswap.com/ws/",
onError: (error) => console.error(error),
});connectionState
Returns the current state of the market WebSocket connection.
const state = marketStream.connectionState;SDK response:
stateconnect
Opens the market WebSocket connection.
const result = await marketStream.connect();After the socket opens, the WebSocket service sends a connected control message:
Calling connect() before subscribing is optional. subscribeOrderBook establishes the connection when necessary.
subscribeOrderBook
Subscribes to market updates for an assetId.
SDK response:
The resolved value is an asynchronous unsubscribe function:
The underlying WebSocket subscription message is:
After the subscription is accepted, the service sends:
unsubscribeOrderBook
Removes all local handlers for an assetId and unsubscribes from that asset’s server-side market feed.
SDK response:
The underlying WebSocket unsubscribe message is:
After the subscription is removed, the service sends:
unsubscribeOrderBook removes every local handler for the asset.
The unsubscribe function returned by subscribeOrderBook removes only the handler associated with that call.
close
Closes the market WebSocket connection.
An optional WebSocket close code and reason can be supplied:
The client does not reconnect after an intentional close unless a new connection or subscription is started.
Order updates
Order updates are delivered to onOrder and onUpdate.
Trade updates
Trade updates are delivered to onTrade and onUpdate.
Cancel updates
Cancel updates are delivered to onCancel and onUpdate.
Resolution updates
Resolution updates are delivered to onResolution and onUpdate.
Error messages
If a subscription message cannot be processed, the service sends an error message:
Errors are delivered to:
The subscription’s
onErrorhandlerThe client-level
onErrorhandler, where applicable
Resynchronization
Market updates include a sequence ID.
If the connection drops or the client determines that the socket is unhealthy, active subscriptions receive:
Reload the complete order-book snapshot through InfoClient before applying subsequent updates:
Subscription behavior
One client can subscribe to multiple asset IDs.
When multiple handlers subscribe to the same asset:
The client creates one server subscription.
Updates are distributed to every local handler.
The returned unsubscribe function removes only its handler.
The server subscription is removed after the last handler unsubscribes.
Reconnection defaults
reconnect
true
reconnectDelayMs
1000
maxReconnectDelayMs
30000
ackTimeoutMs
15000
Heartbeats
The server sends protocol-level WebSocket ping frames.
Browser WebSockets and the Node ws client respond with protocol pong frames automatically.
Do not send an application-level pong message:
Available methods and properties
connectionState
Current connection-state value
connect
Promise<void>
close
void
subscribeOrderBook
Promise<unsubscribe function>
unsubscribeOrderBook
Promise<void>
Last updated