Skip to content

feat(sdk): Add WebSocket subscription support for real-time event streaming - #255

Open
Vincent6581 wants to merge 2 commits into
Kevin737866:mainfrom
Vincent6581:fix/issue-190-websocket-eventstream
Open

feat(sdk): Add WebSocket subscription support for real-time event streaming#255
Vincent6581 wants to merge 2 commits into
Kevin737866:mainfrom
Vincent6581:fix/issue-190-websocket-eventstream

Conversation

@Vincent6581

Copy link
Copy Markdown
Contributor

Summary

Implements a WebSocket-based event subscription client (EventStream) for real-time updates, per issue #190.

The client opens a single WebSocket connection to an event relay and multiplexes any number of typed subscriptions over it, with reconnection, filtering and stale-detection handled transparently.

What's included

Acceptance criterion Status
EventStream class with subscribe(eventType, callback) subscribe(eventType, callback, filter?) returns an unsubscribe fn
Events: Transfer, Trade, DividendClaim, KYCUpdate, CustodyAttestation STREAM_EVENT_TYPES
Auto-reconnect with exponential backoff ✅ configurable base/max delay, multiplier, jitter, attempt cap
Filtering by token_address, user_address ✅ per-subscription EventFilter (single value or list), applied client-side and sent to the relay
Heartbeat / ping for stale detection ✅ periodic ping + stale timer that forces a reconnect

Design notes

  • Injectable transportconfig.webSocketImpl accepts any browser-WebSocket-shaped constructor (the ws package, or a mock WS server in tests). Falls back to the global WebSocket when present.
  • Lazy connect — the socket opens on the first subscribe() call; subscriptions are re-registered automatically on every reconnect.
  • Lifecycle eventsstream.on('open' | 'close' | 'error' | 'reconnecting' | 'stale', listener).
  • Deterministic backoff jitter — jitter is derived from the attempt number (no Math.random) so reconnection timing is testable.
  • eventMatchesFilter() and computeBackoffDelay() are exposed for direct unit testing of the filter and backoff logic.

Files changed

  • sdk/src/eventStream.ts (new)EventStream implementation
  • sdk/src/types.tsStreamEvent, EventFilter, EventStreamConfig, lifecycle + WebSocket structural types
  • sdk/src/index.ts — export EventStream and related types

Usage

import { EventStream } from 'stellar-rwa-sdk';

const stream = new EventStream({ url: 'wss://relay.example/events' });

const off = stream.subscribe(
  'Transfer',
  (e) => console.log('transfer', e.data),
  { tokenAddress: 'C...ABC', userAddress: ['G...1', 'G...2'] }
);

stream.on('reconnecting', ({ attempt, delayMs }) =>
  console.warn(`reconnecting (#${attempt}) in ${delayMs}ms`)
);

// later
off();
stream.close();

Closes #190

…eaming

Adds an EventStream client that multiplexes typed subscriptions over a
single WebSocket connection to an event relay.

- EventStream.subscribe(eventType, callback, filter?) returning an
  unsubscribe function
- Supported events: Transfer, Trade, DividendClaim, KYCUpdate,
  CustodyAttestation
- Auto-reconnect with exponential backoff + jitter and configurable caps
- Per-subscription filtering by token_address / user_address
- Heartbeat ping/pong with stale-connection detection and forced
  reconnect
- Injectable WebSocket implementation (browser global or the `ws`
  package) for testability
- Lifecycle listeners: open, close, error, reconnecting, stale

Files:
- sdk/src/eventStream.ts (new)
- sdk/src/types.ts: stream event / config / lifecycle types
- sdk/src/index.ts: export EventStream and related types

Closes Kevin737866#190
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

@Vincent6581 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

feat(sdk): Add WebSocket subscription support for real-time event streaming

1 participant