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.

npm install @solana/rpc-subscriptions

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:

PackageDescription
@solana/kitIncludes @solana/rpc-subscriptions, @solana/rpc-parsed-types, @solana/rpc-spec-types, and @solana/rpc-types
@solana/rpc-subscriptionsIncludes @solana/rpc-subscriptions-api, @solana/rpc-subscriptions-spec, and utilities for creating default RPC Subscriptions instances
@solana/rpc-subscriptions-apiTypes that describe every method in the Solana RPC WebSocket API
@solana/rpc-subscriptions-specTypes and methods that can be used to create an RPC Subscriptions implementation
@solana/rpc-spec-typesUtility types common to both RPC and RPC Subscriptions implementations
@solana/rpc-transformersHelpers for transforming RPC subscriptions and notifications in various ways appropriate for use in a JavaScript application
@solana/rpc-subscriptions-channel-websocketUtilities for creating custom RPC Subscriptions channels
@solana/rpc-typesTypes 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.

import { ,  } from '@solana/kit';
const  = (('wss://api.mainnet-beta.solana.com'));
const  = await 
    .()
    .({ : .(10_000) });
for await (const  of ) {
    .('The network has advanced to slot', .);
}

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.

On this page