Skip to content

feat(sdk): implement subscribeToContract with WebSocket reconnect#35

Open
Depo-dev wants to merge 1 commit into
devfrom
feat/issue-19-sdk-websocket
Open

feat(sdk): implement subscribeToContract with WebSocket reconnect#35
Depo-dev wants to merge 1 commit into
devfrom
feat/issue-19-sdk-websocket

Conversation

@Depo-dev

@Depo-dev Depo-dev commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

Implements TridentClient.subscribeToContract() with automatic reconnect.

New file: src/subscription.ts

createSubscription(wsUrl, params) function:

  • Opens a native WebSocket to {wsUrl} (derived by replacing http(s):// with ws(s):// + /ws?contractId=...)
  • Validates each inbound message with WsEventSchema (snake_case, matches Go hub's WriteJSON)
  • Parses topics JSON, transforms to SorobanEvent, calls onEvent
  • Invalid frames → calls onError and logs; socket stays open
  • onerror → calls onError
  • Unexpected close (!wasClean) → schedules reconnect with exponential backoff (500ms → 30s)
  • unsubscribe() → sets cancelled = true, clears reconnect timer, closes socket

Updated: src/index.ts

Verification

npm run lint passes (zero TypeScript errors)

Closes #19

Adds src/subscription.ts: createSubscription() opens a native WebSocket
to {wsUrl}, calls onEvent for each valid inbound message (validated
against WsEventSchema and transformed to SorobanEvent), calls onError on
parse failures and connection errors, and reconnects with exponential
backoff (500ms initial, 30s max) on unexpected close. unsubscribe() closes
the socket and cancels any pending reconnect timer.

TridentClient.subscribeToContract() replaces http(s) with ws(s) in
apiUrl, appends contractId and optional topic0 to the query string, and
delegates to createSubscription.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant