> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Listen Keyspace Notifications

Upstash allows you to listen for keyspace notifications over pubsub channels to
receive events for changes over the keys.

For each event that occurs, two kinds of events are fired over the corresponding
pubsub channels:

* A keyspace event that will use the pubsub channel for the key, possibly containing
  other events for the same key
* A keyevent event that will use the pubsub channel for the event, possibly containing
  other events for the different keys

The channel names and their content are of the form:

* `__keyspace@0__:keyname` channel with the values of the event names for the keyspace
  notifications
* `__keyevent@0__:eventname` channel with the values of the key names for the keyevent
  notifications

## Enabling Notifications

By default, all keyspace and keyevent notifications are off. To enable it, you can use
the `CONFIG SET` command, and set the `notify-keyspace-events` options to one of the
appropriate flags described below.

<Warning>
  Each keyspace and keyevent notification fired might have an effect on the latency of the
  commands as the events are delivered to the listening clients and cluster members for
  multi-replica deployments. Therefore, it is advised to only enable the minimal subset of the
  notifications that are needed.
</Warning>

| Flag | Description                 |
| ---- | --------------------------- |
| K    | Keyspace events             |
| E    | Keyevent events             |
| g    | Generic command events      |
| \$   | String command events       |
| l    | List command events         |
| s    | Set command events          |
| h    | Hash command events         |
| z    | Sorted set command events   |
| t    | Stream command events       |
| d    | Module(JSON) command events |
| x    | Expiration events           |
| e    | Eviction events             |
| m    | Key miss events             |
| n    | New key events              |
| A    | Alias for g\$lshztxed       |

At least one of the `K` or `E` flags must be present in the option value.

For example, you can use the following command to receive keyspace notifications
only for the hash commands:

<Tabs>
  <Tab title="cURL">
    ```bash theme={"system"}
    curl -X POST \
        -d '["CONFIG", "SET", "notify-keyspace-events", "Kh"]' \
        -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" \
        $UPSTASH_REDIS_REST_URL
    ```
  </Tab>

  <Tab title="redis-cli">
    ```bash theme={"system"}
    redis-cli --tls -u $UPSTASH_REDIS_CLI_URL config set notify-keyspace-events Kh
    ```
  </Tab>
</Tabs>

You can listen for all the channels using redis-cli to test the effect of the
above command:

```bash theme={"system"}
redis-cli --tls -u $UPSTASH_REDIS_CLI_URL --csv psubscribe '__key*__:*'
```

### Disabling Notifications

You can reuse the `CONFIG SET` command and set `notify-keyspace-events` option to empty string
to disable all keyspace and keyevent notifications.

<Tabs>
  <Tab title="cURL">
    ```bash theme={"system"}
    curl -X POST \
        -d '["CONFIG", "SET", "notify-keyspace-events", ""]' \
        -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" \
        $UPSTASH_REDIS_REST_URL
    ```
  </Tab>

  <Tab title="redis-cli">
    ```bash theme={"system"}
    redis-cli --tls -u $UPSTASH_REDIS_CLI_URL config set notify-keyspace-events ""
    ```
  </Tab>
</Tabs>

### Checking Notification Configuration

`CONFIG GET` command can be used the get the current value of the `notify-keyspace-events` option
to see the active keyspace and keyevent notifications configuration.

<Tabs>
  <Tab title="cURL">
    ```bash theme={"system"}
    curl -X POST \
        -d '["CONFIG", "GET", "notify-keyspace-events"]' \
        -H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" \
        $UPSTASH_REDIS_REST_URL
    ```
  </Tab>

  <Tab title="redis-cli">
    ```bash theme={"system"}
    redis-cli --tls -u $UPSTASH_REDIS_CLI_URL config get notify-keyspace-events
    ```
  </Tab>
</Tabs>
