PendingRpcSubscriptionsRequest

type PendingRpcSubscriptionsRequest<TNotification> = object;

Pending subscriptions are the result of calling a supported method on a RpcSubscriptions object. They encapsulate all of the information necessary to make the subscription without actually making it.

Calling the `subscribe(options)` method on a PendingRpcSubscriptionsRequest<TNotification> will trigger the subscription and return a promise for an async iterable that vends TNotifications.

Calling the `reactiveStore(options)` method will return a ReactiveStreamStore compatible with useSyncExternalStore, Svelte stores, and other reactive primitives.

Type Parameters

Type Parameter
TNotification

Methods

reactive()

reactive(options): Promise<ReactiveStreamStore<TNotification>>;

Triggers the subscription and returns a promise for a ReactiveStreamStore that holds the latest notification. Compatible with useSyncExternalStore and other reactive primitives that expect a { subscribe, getState } contract.

Parameters

ParameterType
optionsRpcSubscribeOptions

Returns

Promise<ReactiveStreamStore<TNotification>>

Example

const store = await rpc.accountNotifications(address).reactive({ abortSignal });
// React — throw error from snapshot to surface via Error Boundary
const state = useSyncExternalStore(store.subscribe, () => {
    if (store.getError()) throw store.getError();
    return store.getState();
});

Deprecated

Use `reactiveStore()` instead. The synchronous variant returns a store that reconnects on ReactiveStreamStore.retry | `retry()` after an error, whereas the store returned by reactive() cannot recover once its underlying DataPublisher has failed.


reactiveStore()

reactiveStore(options): ReactiveStreamStore<TNotification>;

Synchronously returns a ReactiveStreamStore that subscribes in the background and holds the latest notification. Compatible with useSyncExternalStore and other reactive primitives that expect a { subscribe, getUnifiedState } contract. The store opens a fresh subscription on construction and on every ReactiveStreamStore.retry | `retry()`.

Parameters

ParameterType
optionsRpcSubscribeOptions

Returns

ReactiveStreamStore<TNotification>

Example

const store = rpc.accountNotifications(address).reactiveStore({ abortSignal });
// React — the unified snapshot has stable identity per update.
const state = useSyncExternalStore(store.subscribe, store.getUnifiedState);
if (state.status === 'error') return <ErrorMessage error={state.error} onRetry={store.retry} />;
if (state.status === 'loading') return <Spinner />;
return <View data={state.data} />;

subscribe()

subscribe(options): Promise<AsyncIterable<TNotification, any, any>>;

Triggers the subscription and returns a promise for an async iterable of notifications. Use for await...of to consume notifications as they arrive. Abort the signal to unsubscribe.

Parameters

ParameterType
optionsRpcSubscribeOptions

Returns

Promise<AsyncIterable<TNotification, any, any>>

Example

const notifications = await rpc.accountNotifications(address).subscribe({ abortSignal });
for await (const notification of notifications) {
    console.log('Account changed:', notification);
}

On this page