RPC subscriptions
Get notified of changes to the blockchain
Introduction
Kit offers a TypeScript interface that you can use to subscribe for notifications from a Solana JSON RPC server. Registering subscriptions with a RPC server will yield notifications over that channel when data matching your subscription is updated.
Kit aims to implement all of the RPC Subscription methods documented in the Solana RPC WebSocket methods docs. You can either browse the available methods in the docs, or you can follow Kit's TypeScript types and inline documentation in your editor.
Installation
Functions for creating RPC Subscriptions clients are included within the @solana/kit
library but you may also install them using their standalone package.
Note that the @solana/rpc-subscriptions
package itself is composed of several smaller packages, each with a distinct responsibility. Here's the list of all packages that come together to implement the default RPC Subscriptions client should you wish to create your own custom one:
Package | Description |
---|---|
@solana/kit | Includes @solana/rpc-subscriptions , @solana/rpc-parsed-types , @solana/rpc-spec-types , and @solana/rpc-types |
@solana/rpc-subscriptions | Includes @solana/rpc-subscriptions-api , @solana/rpc-subscriptions-spec , and utilities for creating default RPC Subscriptions instances |
@solana/rpc-subscriptions-api | Types that describe every method in the Solana RPC WebSocket API |
@solana/rpc-subscriptions-spec | Types and methods that can be used to create an RPC Subscriptions implementation |
@solana/rpc-spec-types | Utility types common to both RPC and RPC Subscriptions implementations |
@solana/rpc-transformers | Helpers for transforming RPC subscriptions and notifications in various ways appropriate for use in a JavaScript application |
@solana/rpc-subscriptions-channel-websocket | Utilities for creating custom RPC Subscriptions channels |
@solana/rpc-types | Types for values used in the Solana Websocket API and a series of helpers for working with them |
What is a RPC?
A RPC is a server with an up-to-date view of the Solana blockchain. You can send it subscriptions to request notifications for changes to things like account data, signature statuses, and slot progression.
Here's a quick example that subscribes for a notification every time the network's slot count advances, then unsubscribes after 10 seconds.
Most RPC servers implement most of the Solana WebSocket API covered by Kit, but some offer extra methods above and beyond that. Those that do should offer an SDK that you can use to wrap Kit's RPC WebSocket API implementation, merging the method implementations and TypeScript types of both APIs into a single object.
Where to find an RPC
For light development or personal use, you can use a public RPC server for the cluster you want to access.
wss://api.mainnet-beta.solana.com
wss://api.testnet.solana.com
wss://api.devnet.solana.com
When deploying your application to production, you will want to lease an RPC server or set up your own.