Oracle Methods
OracleWebSocketClient manages the oracle WebSocket connection and subscribes to real-time price updates by symbolId.
import {
createOracleWebSocketClient,
} from "@gammaswap/v2-exchange-sdk";
const oracleStream = createOracleWebSocketClient({
websocketUrl: "wss://exchange-api.gammaswap.com/oracle-ws/",
stalePriceTimeoutMs: 30_000,
onError: (error) => console.error(error),
});connectionState
Returns the current state of the oracle WebSocket connection.
const state = oracleStream.connectionState;connect
Opens the oracle WebSocket connection.
const result = await oracleStream.connect();After the socket opens, the WebSocket service sends a connected control message:
{
"type": "connected",
"message": "Send {\"type\":\"subscribe\",\"symbolId\":\"...\"} to receive prices"
}Calling connect() before subscribing is optional. subscribePrice establishes the connection when necessary.
subscribePrice
Subscribes to live oracle prices for a symbolId.
The resolved value is an asynchronous unsubscribe function:
The underlying WebSocket subscription message is:
After the subscription is accepted, the service sends:
Price updates
Price updates are delivered to the subscription’s onPrice handler.
The update contains:
type
string
Always price for a price update
symbolId
string
Subscribed oracle symbol ID
price
string
Current oracle price
ts
number
Price timestamp
Example handler:
Handler input:
unsubscribePrice
Removes all local handlers for a symbolId and unsubscribes from that symbol’s server-side price feed.
The underlying WebSocket unsubscribe message is:
After the subscription is removed, the service sends:
unsubscribePrice removes every local handler for the symbol.
The unsubscribe function returned by subscribePrice removes only the handler associated with that call.
Unavailable symbols
If a symbol becomes unavailable after subscription, the service can send:
The symbol is no longer subscribed after this message.
close
Closes the oracle 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.
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
Stale prices
stalePriceTimeoutMs defines the maximum time the client waits without receiving a price update for a subscribed symbol.
The default is:
If no price arrives before the timeout, the client calls:
The handler receives the stale symbolId:
The client also:
Emits an error.
Abandons the unhealthy socket.
Reconnects when active subscriptions remain.
The oracle stream does not provide sequence IDs or REST catch-up.
After reconnecting or receiving onStale, accept the next live price update for the symbol.
Subscription behavior
One client can subscribe to multiple symbol IDs.
When multiple handlers subscribe to the same symbol:
The client creates one server subscription.
Price 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
stalePriceTimeoutMs
30000
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
subscribePrice
Promise<unsubscribe function>
unsubscribePrice
Promise<void>
Last updated