sonos

Subscribing to events with Sonos API


I'm in the process of building a Sonos iPhone app with Swift using the Sonos API but have hit a stumbling block. I would like to detect when a state/volume / metadata change on my app when someone uses a different app or the hardware buttons. I believe that subscribing to events is the way forward. I have copied in the reference link below.

https://developer.sonos.com/build/direct-control/connect/

I send a HTTP POST to the subscription path as described and get a 200 ok response back but have no idea what to do next. I've been using Xcode and testing in Postman. The callback url is registered on the developer site and OAuth token is verified.

Any help would be greatly appreciated.

Thanks, Paul


Solution

  • You must host a service to receive events. Your server should satisfy all of these requirements:

    Your event service should also verify the X-Sonos-Event-Signature of each event it receives. See Subscribe on the Sonos developer portal for details.

    Enter your callback URL in the form field for your Control integration.

    You should receive an HTTP POST to your callback URL when states change in the namespace to which you subscribed. For example, if you subscribed to the groupVolume namespace for a group, you should receive a group volume event when the group volume changes. Try changing the group volume in the Sonos app and verify that your callback URL gets an event.

    Here's a sample group volume event from the documentation, sent after a change in the volume state:

        POST {YOUR_CALLBACK_BASE_URL} HTTP/1.1
        Content-Type: application/json
        Content-Length: 41
        X-Sonos-Household-Id: Sonos_1234567890
        X-Sonos-Event-Seq-Id: 1234
        X-Sonos-Event-Signature: 90192js8cjhvd7ebcjsfdfw
        X-Sonos-Namespace: groupVolume
        X-Sonos-Type: groupVolume
        X-Sonos-Target-Type: groupId
        X-Sonos-Target-Value: RINCON_00012345678001400:0
        {"volume":16,"muted":false,"fixed":false}