A generic implementation of JSON RPC Subscriptions using proxies
npm install @solana/rpc-subscriptions-spec[![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
[code-style-prettier-url]: https://github.com/prettier/prettier
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/rpc-subscriptions-spec?style=flat
[npm-image]: https://img.shields.io/npm/v/@solana/rpc-subscriptions-spec?style=flat
[npm-url]: https://www.npmjs.com/package/@solana/rpc-subscriptions-spec
This package contains types that describe the implementation of the JSON RPC Subscriptions API, as well as methods to create one. It can be used standalone, but it is also exported as part of Kit @solana/kit.
This API is designed to be used as follows:
``tsRpcSubscriptions
const rpcSubscriptions =
// Step 1 - Create a instance. This may be stateful.PendingRpcSubscriptionsRequest
createSolanaRpcSubscriptions(mainnet('wss://api.mainnet-beta.solana.com'));
const response = await rpcSubscriptions
// Step 2 - Call supported methods on it to produce objects.subscribe()
.slotNotifications({ commitment: 'confirmed' })
// Step 3 - Call the method on those pending requests to trigger them.`
.subscribe({ abortSignal: AbortSignal.timeout(10_000) });
// Step 4 - Iterate over the result.
try {
for await (const slotNotification of slotNotifications) {
console.log('Got a slot notification', slotNotification);
}
} catch (e) {
console.error('The subscription closed unexpectedly', e);
} finally {
console.log('We have stopped listening for notifications');
}
A channel is a DataPublisher on which you can subscribe to events of type RpcSubscriptionChannelEvents. Additionally, you can use this object to send messages of type TOutboundMessage back to the remote end by calling its send(message) method.
A channel creator is a function that accepts an AbortSignal, returns a new RpcSubscriptionsChannel, and tears down the channel when the abort signal fires.
Subscription channels publish events on two channel names:
- error: Fires when the channel closes unexpectedlymessage
- : Fires on every message received from the remote end
Given a channel, this function executes the particular subscription plan required by the Solana JSON RPC Subscriptions API.
1. Calls the subscribeRequest on the remote RPCDataPublisher
2. Waits for a response containing the subscription id
3. Returns a that publishes notifications related to that subscriptions id, filtering out all othersunsubscribeMethodName
4. Calls the on the remote RPC when the abort signal is fired.
Given a channel with inbound messages of type T and a function of type T => U, returns a new channel with inbound messages of type U. Note that this only affects messages of type "message" and thus, does not affect incoming error messages.
For instance, it can be used to parse incoming JSON messages:
`ts`
const transformedChannel = transformChannelInboundMessages(channel, JSON.parse);
Given a channel with outbound messages of type T and a function of type U => T, returns a new channel with outbound messages of type U.
For instance, it can be used to stringify JSON messages before sending them over the wire:
`ts``
const transformedChannel = transformChannelOutboundMessages(channel, JSON.stringify);