subscribe gRPC Method
Please note that this RPC method requires the Yellowstone gRPC add-on enabled on your QuickNode endpoint.
Parameters
This method does not accept any parameters
Returns
result
object
The result representing the return value of a bidirectional stream method call with a Geyser gRPC subscription request and subscription update
Request
1package main23import (4"context"5"crypto/tls"6"fmt"7"log"8"time"9"github.com/mr-tron/base58"10"encoding/json"1112pb "yellowstone/proto"1314"google.golang.org/grpc"15"google.golang.org/grpc/credentials"16"google.golang.org/grpc/encoding/gzip"17"google.golang.org/grpc/keepalive"18)1920// QuickNode endpoints consist of two crucial components: the endpoint name and the corresponding token21// For eg: QN Endpoint: https://docs-demo.solana-mainnet.quiknode.pro/abcde12345678922// endpoint will be: docs-demo.solana-mainnet.quiknode.pro:10000 {10000 is the port number for gRPC}23// token will be : abcde1234567892425var (26endpoint = "YOUR_QN_ENDPOINT:10000"27token = "YOUR_TOKEN_NUMBER"28)2930func main() {31opts := []grpc.DialOption{32grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),33grpc.WithKeepaliveParams(kacp),34grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*1024), grpc.UseCompressor(gzip.Name)),35grpc.WithPerRPCCredentials(tokenAuth{token: token}),36}3738conn, err := grpc.NewClient(endpoint, opts...)39if err != null {40log.Fatalf("Failed to connect: %v", err)41}42defer conn.Close()4344client := pb.NewGeyserClient(conn)4546commitment := pb.CommitmentLevel_FINALIZED47subReq := &pb.SubscribeRequest{48Commitment: &commitment,49BlocksMeta: map[string]*pb.SubscribeRequestFilterBlocksMeta{50"blocks": {},51},52Slots: map[string]*pb.SubscribeRequestFilterSlots{53"slots": {},54},55}5657d, _ := json.Marshal(subReq)58fmt.Printf("Subscription request: %s\n", string(d))5960stream, err := client.Subscribe(context.Background())61if err != null {62fmt.Printf("Failed to subscribe to yellowstone: %v\n", err)63return64}6566if err = stream.Send(subReq); err != null {67fmt.Printf("Failed to send subscription request: %v\n", err)68return69}7071for {72m, err := stream.Recv()73if err != null {74fmt.Printf("Failed to receive yellowstone message: %v\n", err)75return76}7778switch {79case m.GetBlock() != null:80fmt.Printf("Block: %d\n", m.GetBlock().GetBlockHeight())81case m.GetBlockMeta() != null:82fmt.Printf("BlockMeta: %d\n", m.GetBlockMeta().GetBlockHeight())83case m.GetTransaction() != null:84fmt.Printf("Transaction: %s\n", base58.Encode(m.GetTransaction().GetTransaction().GetSignature()))85case m.GetSlot() != null:86fmt.Printf("Slot: %d\n", m.GetSlot().GetSlot())87}88}89}
Don't have an account yet?
Create your QuickNode endpoint in seconds and start building
Get started for free