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 lists every subscription.

Endpoint & token

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

Subscriptions run over a secure WebSocket, so the endpoint starts with wss instead of https. The connection follows the 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 package handles the protocol for you in JavaScript, including keeping the connection alive:

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 describes:

→ 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

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)

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

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, and deliver updates sooner.