Skip to content

Commit 9b35551

Browse files
LukasDeco0xbigz
authored andcommitted
lukas/websocket improvements (#1807)
* feat: initial implementation for users and markets WS improvements * lukas/gill websocket sub (#1781) * websockets gill temp * feat: feature parity between gill version ws acct sub and reg one + optional passing into driftClient * fix: post rebase bugs and cleanup * chore: websocket account subscriber export * feat: logging string update on ws acct v2 * rm: useless logging * chore: cleanup ws subscriber v2 docs * chore: specific name on custom ws acct sub param * fix: post rebase again cleanup * fix: prettier fixed * feat: initial implementation for users and markets WS improvements * feat: polling check on websocket acct subscriber v2 + naming * fix: lint * fix: non-hanging WS subscription async loop handling * fix: bugs with program ws subs hanging on asynciter * fix: goofy self imports * feat: initial batch fetching temp * temp: sub second WS subscribe time * fix: ws program account subscriber v2 bugs and optimizations * feat: chunk stuff account requests * feat: more subscribe optimizations ws driftclient sub v2 * chore: cleanup ws sub v2 logs * feat: conditional check on using ws account subscriber + unused * fix: bad import * chore: add export of WebSocketProgramAccountSubscriberV2 * fix: unneeded drift idl export messing up common build * fix: consolidate rpc ws subscriptions for oracles * feat: docs for ws v2 and cleanup * chore: more docs on ws acct susbcriber v2 * feat: PR feedback round 2 * fix: default timeout for ws v2 susbcribers * feat: PR feedback on resubOpts and simplify logic * fix: prettier
1 parent e4e5820 commit 9b35551

14 files changed

+2228
-662
lines changed

sdk/src/accounts/README_WebSocketAccountSubscriberV2.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,46 @@ await subscriber.subscribe((data) => {
3131
await subscriber.unsubscribe();
3232
```
3333

34+
### Polling Instead of Resubscribing
35+
36+
For accounts that rarely update (like long-tail markets), you can use polling instead of resubscribing to reduce resource usage:
37+
38+
```typescript
39+
const resubOpts = {
40+
resubTimeoutMs: 30000, // 30 seconds
41+
logResubMessages: true,
42+
usePollingInsteadOfResub: true, // Enable polling mode
43+
pollingIntervalMs: 30000, // Poll every 30 seconds (optional, defaults to 30000)
44+
};
45+
46+
const subscriber = new WebSocketAccountSubscriberV2(
47+
'perpMarket', // account name
48+
program,
49+
marketPublicKey,
50+
undefined, // decodeBuffer
51+
resubOpts
52+
);
53+
```
54+
55+
**How it works:**
56+
1. Initially subscribes to WebSocket updates
57+
2. If no WebSocket data is received for `resubTimeoutMs` (30s), switches to websocket+polling mode if `usePollingInsteadOfResub` is specified true, else just resubscribes(unsub, sub).
58+
3. Polls every `pollingIntervalMs` (alongside websocket connection) to check for updates by:
59+
- Storing current account buffer state
60+
- Fetching latest account data
61+
- Comparing buffers to detect any missed updates
62+
4. If polling detects new data (indicating missed WebSocket events):
63+
- Immediately stops polling
64+
- Resubscribes to WebSocket to restore real-time updates
65+
- This helps recover from degraded WebSocket connections
66+
5. If a WebSocket event is received while polling:
67+
- Polling is automatically stopped
68+
- System continues with normal WebSocket updates
69+
6. This approach provides:
70+
- Efficient handling of rarely-updated accounts
71+
- Automatic recovery from WebSocket connection issues
72+
- Seamless fallback between polling and WebSocket modes
73+
3474
## Implementation Details
3575

3676
### Gill Integration
@@ -52,3 +92,4 @@ const { rpc, rpcSubscriptions } = createSolanaClient({
5292
3. **Address Handling**: Converts `PublicKey` to gill's `Address` type for compatibility
5393
4. **Response Formatting**: Converts gill responses to match the expected `AccountInfo<Buffer>` format
5494
5. **Abort Signal**: Utilizes AbortSignal nodejs/web class to shutdown websocket connection synchronously
95+
6. **Polling Mode**: Optional polling mechanism for accounts that rarely update

sdk/src/accounts/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ export type DataAndSlot<T> = {
202202
export type ResubOpts = {
203203
resubTimeoutMs?: number;
204204
logResubMessages?: boolean;
205+
// New options for polling-based resubscription
206+
usePollingInsteadOfResub?: boolean;
207+
pollingIntervalMs?: number;
205208
};
206209

207210
export interface UserStatsAccountEvents {

0 commit comments

Comments
 (0)