Thankfully, a quick search yielded an excellent package called sonos on GitHub, put together by the delightful Ben Evans (cheers, Ben).

With that, I cloned my ES6 boilerplate, fired up Sublime, and got to work.

A quick dive into the library files helped me break things down a little:

Pass the Spotify track ID from the client to the server. Find the Sonos instance on the network. If it can find one, connect to it and queue the track. Skip to the next track. Play!

Well, that seems fairly straightforward…

…however, once I’d got my teeth stuck in, I ended up encountering random 500 errors on the play method that seemed to derive from the Sonos response itself.

After trawling through endless GitHub issue after GitHub issue, emitting occasional frustrated grunts, I had an epiphany:

This was just a little experiment. Time was fairly short.

Get on with it.

Instead of cueing something up after whatever’s playing currently, I found I could add the track to the beginning of the Sonos queue (position 1), jump to that position, and then play from there.

I also found that I needed to perform an additional step; selectQueue first. If the queue isn’t active (for example, the Sonos is playing a radio station), we need to switch to the queue before we can add our entrance music.

Oh, and for funsies, I threw in another additional step — turn the volume up to 60% before the track starts playing. 🤙

A quick code revision later:

I set up the routes, fired off a request, and bingo.

The Final Countdown victoriously blasted across the second floor of The Tea Building in the middle of (what was) a chilled-out Thursday afternoon.

With my hands raised in the air in victory, I glanced around at the small group of disgruntled-looking individuals now reaching for and plunging earphones into their lobes.

I decided to keep future victories strictly internal.

Beacons