# slotSubscribe

> Subscribe to notifications when the Solana validator processes a new slot.

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

The `slotSubscribe` method opens a stream that emits a notification each time the validator processes a new slot. Pair it with [`slotUnsubscribe`](#unsubscribe) to stop receiving notifications.

# Parameters

* None

# Request

<CodeGroup>
  ```shell wscat
  // initiate websocket stream first
  wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->

  // then call subscription
  {"jsonrpc":"2.0","id":1,"method":"slotSubscribe"}
  ```

  ```javascript @solana/web3.js
  import { Connection } from '@solana/web3.js'

  const connection = new Connection(
    'https://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->',
    {
      wsEndpoint: 'wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->'
    }
  )

  const subscriptionId = connection.onSlotChange((slotInfo) => {
    console.log('Slot update:', {
      slot: slotInfo.slot,
      parent: slotInfo.parent,
      root: slotInfo.root
    })
  })

  // To unsubscribe later:
  // await connection.removeSlotChangeListener(subscriptionId)
  ```
</CodeGroup>

# Result

<CodeGroup>
  ```json result
  // subscribe response
  {"jsonrpc":"2.0","result":0,"id":1}

  // notification
  {
    "jsonrpc": "2.0",
    "method": "slotNotification",
    "params": {
      "result": {
        "parent": 75,
        "root": 42,
        "slot": 76
      },
      "subscription": 0
    }
  }
  ```
</CodeGroup>

# Unsubscribe

Use `slotUnsubscribe` with the subscription id returned by `slotSubscribe` to cancel the stream.

* `subscription_id`: `number` - The subscription id to cancel.

<CodeGroup>
  ```shell wscat
  {"jsonrpc":"2.0","id":1,"method":"slotUnsubscribe","params":[subscription_id]}
  ```
</CodeGroup>

```json result
{"jsonrpc":"2.0","result":true,"id":1}
```

When using `@solana/web3.js`, call `connection.removeSlotChangeListener(subscriptionId)` instead of sending the raw JSON-RPC request.