Skip to content

lukas/websocket improvements #1807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c093ff9
feat: initial implementation for users and markets WS improvements
LukasDeco Jul 25, 2025
3796028
lukas/gill websocket sub (#1781)
LukasDeco Jul 30, 2025
d50642a
feat: initial implementation for users and markets WS improvements
LukasDeco Jul 25, 2025
7217cc7
feat: polling check on websocket acct subscriber v2 + naming
LukasDeco Jul 31, 2025
3b50c7a
fix: lint
LukasDeco Jul 31, 2025
28ff6bf
fix: non-hanging WS subscription async loop handling
LukasDeco Aug 1, 2025
812ec08
fix: bugs with program ws subs hanging on asynciter
LukasDeco Aug 5, 2025
5ee6f49
fix: goofy self imports
LukasDeco Aug 6, 2025
1a0ecf2
feat: initial batch fetching temp
LukasDeco Aug 6, 2025
da84940
temp: sub second WS subscribe time
LukasDeco Aug 7, 2025
3843a20
fix: ws program account subscriber v2 bugs and optimizations
LukasDeco Aug 8, 2025
dc2064d
feat: chunk stuff account requests
LukasDeco Aug 8, 2025
9815c48
feat: more subscribe optimizations ws driftclient sub v2
LukasDeco Aug 11, 2025
bc1fcfa
chore: cleanup ws sub v2 logs
LukasDeco Aug 11, 2025
3577582
feat: conditional check on using ws account subscriber + unused
LukasDeco Aug 12, 2025
ef08f7e
fix: bad import
LukasDeco Aug 12, 2025
30862b8
chore: add export of WebSocketProgramAccountSubscriberV2
LukasDeco Aug 12, 2025
7195770
fix: unneeded drift idl export messing up common build
LukasDeco Aug 15, 2025
6c18e01
fix: consolidate rpc ws subscriptions for oracles
LukasDeco Aug 15, 2025
9351341
feat: docs for ws v2 and cleanup
LukasDeco Aug 15, 2025
f635fa5
chore: more docs on ws acct susbcriber v2
LukasDeco Aug 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions sdk/src/accounts/README_WebSocketAccountSubscriberV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ await subscriber.subscribe((data) => {
await subscriber.unsubscribe();
```

### Polling Instead of Resubscribing

For accounts that rarely update (like long-tail markets), you can use polling instead of resubscribing to reduce resource usage:

```typescript
const resubOpts = {
resubTimeoutMs: 30000, // 30 seconds
logResubMessages: true,
usePollingInsteadOfResub: true, // Enable polling mode
pollingIntervalMs: 30000, // Poll every 30 seconds (optional, defaults to 30000)
};

const subscriber = new WebSocketAccountSubscriberV2(
'perpMarket', // account name
program,
marketPublicKey,
undefined, // decodeBuffer
resubOpts
);
```

**How it works:**
1. Initially subscribes to WebSocket updates
2. If no WebSocket data is received for `resubTimeoutMs` (30s), switches to polling mode if `usePollingInsteadOfResub` is specified true, else just resubscribes(unsub, sub).
3. Polls every `pollingIntervalMs` (30s) to check for updates by:
- Storing current account buffer state
- Fetching latest account data
- Comparing buffers to detect any missed updates
4. If polling detects new data (indicating missed WebSocket events):
- Immediately stops polling
- Resubscribes to WebSocket to restore real-time updates
- This helps recover from degraded WebSocket connections
5. If a WebSocket event is received while polling:
- Polling is automatically stopped
- System continues with normal WebSocket updates
6. This approach provides:
- Efficient handling of rarely-updated accounts
- Automatic recovery from WebSocket connection issues
- Seamless fallback between polling and WebSocket modes

## Implementation Details

### Gill Integration
Expand All @@ -52,3 +92,4 @@ const { rpc, rpcSubscriptions } = createSolanaClient({
3. **Address Handling**: Converts `PublicKey` to gill's `Address` type for compatibility
4. **Response Formatting**: Converts gill responses to match the expected `AccountInfo<Buffer>` format
5. **Abort Signal**: Utilizes AbortSignal nodejs/web class to shutdown websocket connection synchronously
6. **Polling Mode**: Optional polling mechanism for accounts that rarely update
3 changes: 3 additions & 0 deletions sdk/src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ export type DataAndSlot<T> = {
export type ResubOpts = {
resubTimeoutMs?: number;
logResubMessages?: boolean;
// New options for polling-based resubscription
usePollingInsteadOfResub?: boolean;
pollingIntervalMs?: number;
};

export interface UserStatsAccountEvents {
Expand Down
Loading
Loading