Program Streams Subscribe

Subscribe accepts one topic and one output format.

Request

protobuf
message SubscribeRequest {
  string topic = 1;
  OutputFormat format = 2;
}

Response

protobuf
message SubscribeResponse {
  string topic = 1;
  uint64 slot = 2;
  string payload = 5;
}

JavaScript

javascript
import grpc from "@grpc/grpc-js"
import protoLoader from "@grpc/proto-loader"

const definition = protoLoader.loadSync("program-streams.proto", {
  longs: String,
  enums: String,
  defaults: true,
})

const root = grpc.loadPackageDefinition(definition)
const Service = root.nln.stream.v1.StreamService
const client = new Service(
  "events.sol.cubestech.online:443:443",
  grpc.credentials.createSsl(),
)

const metadata = new grpc.Metadata()
metadata.set("x-api-key", process.env.NODE_API_KEY)

const stream = client.Subscribe(
  {
    topic: "solana.pump_fun.trade_event",
    format: "JSON",
  },
  metadata,
)

stream.on("data", (message) => {
  const payload = JSON.parse(message.payload)
  console.log(message.slot, payload)
})

stream.on("error", (error) => {
  console.error(error.code, error.details)
})

Close the client stream to unsubscribe.

Consumer rules

  • Store slot.
  • Parse JSON outside the network callback when work is expensive.
  • Bound your queue.
  • Pause or restart before memory grows without limit.
  • Deduplicate downstream writes.