# Program Streams Subscribe

> Open an authenticated server stream for one decoded program topic.

Canonical: https://nolimitnodes.com/docs/api-reference/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

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.nln.clr3.org:443",
  grpc.credentials.createSsl(),
)

const metadata = new grpc.Metadata()
metadata.set("x-api-key", process.env.NLN_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.
