Link Search Menu Expand Document

Getting started

Using this library is really easy.

  1. Add the library to your project
  2. Use Sonos manager
    1. or control single device
  3. Debugging issues
  4. Music services regions
  5. Apple music region
    1. Deezer region
    2. Spotify region

Add the library to your project

npm install --save @svrooij/sonos

Use Sonos manager

This library is developed with Sonos groups in mind. We created a SonosManager to discover all known groups and keep track of changes to them.

const SonosManager = require('@svrooij/sonos').SonosManager
const manager = new SonosManager()
manager.InitializeWithDiscovery(10)
  .then(console.log)
  .then(() => {
    manager.Devices.forEach(d => console.log('Device %s (%s) is joined in %s', d.Name, d.uuid, d.GroupName))
  })
  .catch(console.error)

In some network situations (or Docker usage) SSDP won’t work, but you can also start the manager if you know one (static) IP of a single speaker.

const SonosManager = require('@svrooij/sonos').SonosManager
const manager = new SonosManager()
manager.InitializeFromDevice(process.env.SONOS_HOST || '192.168.96.56')
  .then(console.log)
  .then(() => {
    manager.Devices.forEach(d => console.log('Device %s (%s) is joined in %s', d.Name, d.uuid, d.GroupName))
  })
  .catch(console.error)

Using the manager will also make sure the players in a group will have two new TransportState values, GROUP_PLAYING and GROUP_STOPPED.

or control single device

If you just want to control a single device and don’t want to use the SonosManager, you can also create a instance of SonosDevice, but you’ll be missing the group options.

const SonosDevice = require('@svrooij/sonos').SonosDevice

const sonos = new SonosDevice(process.env.SONOS_HOST || '192.168.96.56')
sonos.LoadDeviceData()
  .then(success => {
    console.log(sonos.Name)
  })
  .catch(console.error)

Debugging issues

This library uses the debug library. If you want a lot more information about what is happening you can turn on all debug messages by setting the environment variable DEBUG to sonos:*.

All logging information might be a bit overkill, it you set DEBUG to sonos:eventListener you will only receive log information from the event listener.

Debug setting What is logged
sonos:* Everything is logged to the console
sonos:eventListener Messages about the event listener
sonos:metadata Messages about the MetaDataHelper
sonos:service:* All services logs will be emitted
sonos:service:{serviceName}:* All logs about a specific service will be emitted
sonos:service:{serviceName}:{ip} All messages for a specific service for a specific speaker.

Check out the debug library for more configuration options. You can also combine several instructions with a , or just remove a specif event with - in front.

Music services regions

This library guesses the required metadata from some music services. A few services need a mandatory region, which we use a sensible default for, but you should override if required.

Apple music region

Environment variable: SONOS_REGION_APPLE

Value: 52231 (default)

Deezer region

Environment variable: SONOS_REGION_DEEZER

Value: 519 (default)

Spotify region

Environment variable: SONOS_REGION_SPOTIFY

Values: 2311 for Europe (default), 3079 USA?