# Subscriptions


Use subscriptions for information you expect to change often, such as what's playing, whether the zone is playing or paused, and the zone's current errors.

The [API reference](/api/graphql-reference) lists every subscription.

## Endpoint & token

```plaintext
wss://api.soundtrack.io/v2/graphql-transport-ws
```

Subscriptions run over a secure [WebSocket](https://en.wikipedia.org/wiki/WebSocket), so the endpoint starts with `wss` instead of `https`. The connection follows the [graphql-ws](https://github.com/enisdenjo/graphql-ws) protocol, so open it with the `graphql-transport-ws` subprotocol.

Send your token in the first message, `connection_init`, in the same form as the `Authorization` header: `Basic your_token`. The token never goes in the URL.

## Connecting

The [graphql-ws](https://github.com/enisdenjo/graphql-ws) package handles the protocol for you in JavaScript, including keeping the connection alive:

```js
import { createClient } from 'graphql-ws'

const client = createClient({
  url: 'wss://api.soundtrack.io/v2/graphql-transport-ws',
  connectionParams: { Authorization: 'Basic your_token' },
})

client.subscribe(
  {
    query: `subscription NowPlayingUpdate($soundZone: ID!) {
      nowPlayingUpdate(input: { soundZone: $soundZone }) {
        nowPlaying { track { title } }
      }
    }`,
    variables: { soundZone: 'sound_zone_id' },
    operationName: 'NowPlayingUpdate',
  },
  {
    next: ({ data }) => console.log(data),
    error: (error) => console.error(error),
    complete: () => console.log('done'),
  },
)
```

In another language, or with your own client, send the messages the [protocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) describes:

```text
→ connection_init  { "Authorization": "Basic your_token" }
← connection_ack
→ subscribe        { "query": …, "variables": …, "operationName": … }
← next             { "data": … }   each time something changes
→ complete         when you no longer need updates
```

## Example

```graphql
subscription NowPlayingUpdate($soundZone: ID!) {
  nowPlayingUpdate(input: { soundZone: $soundZone }) {
    nowPlaying {
      track {
        title
        album {
          title
          display {
            image {
              size(width: 640, height: 640)
            }
          }
        }
        artists {
          name
        }
      }
    }
  }
}
```

> The `data` in each `next` message (it arrives again each time something changes)

```json
{
  "data": {
    "nowPlayingUpdate": {
      "nowPlaying": {
        "track": {
          "title": "Needs",
          "artists": [
            {
              "name": "Loure"
            }
          ],
          "album": {
            "title": "Smooth Talk EP",
            "display": {
              "image": {
                "size": "https://theurltothealbumartwork.com/07dc5282"
              }
            }
          }
        }
      }
    }
  }
}
```

:::note
cURL doesn't support WebSockets out of the box, so there's no cURL example here. To try subscriptions right away, use the [API explorer](https://api.soundtrack.io/v2/explore).
:::

This example gets what a screen showing what's playing needs: the track and album titles, the artists' names and the album art, sized to 640 by 640 pixels.

The response updates each time something changes.

## Handling the first value and reconnects

To show what's playing as soon as a user opens your app, send an initial query for what's playing, then let the subscription take over from there.

If the connection drops, you miss the changes made while you were away. When you reconnect, send the query again to catch up, then subscribe again. The graphql-ws package reconnects on its own, so you only need to send the query.

Prefer subscriptions over repeated queries: they limit the number of requests, help you avoid getting [rate limited](/api/errors-and-rate-limits#rate-limit), and deliver updates sooner.
