Skip to main content
Headliner II
March 5, 2013

Sonos with node.js, my attempt!

INFO

Source code here: https://github.com/jishi/node-sonos-http-api
Sonos Web Controller draft (work in progress!): https://github.com/jishi/node-sonos-web-controller
Sonos IR control (requires lircd and IR reciever): https://github.com/jishi/node-sonos-remote-control

==============================================================


I know that there already is an attempt at this by this guy: http://forums.sonos.com/showthread.php?t=32643, but I found it to be a bit lacking in functionality so I decided to write up my own.

I have created a simple web-based API using what I have, which could be useful for integrating stuff with other applications. This web based API is inspired by other RESTful APIs, however I don't think it follows the correct guidelines to be called a REST API.

IT supports most basic features like:

play, pause, seek, next, prev, volume, mute, setAVTransportURI

It also supports these advanced fatures:

State of player as JSON, zone info as JSON, Play favorite item, presets (grouping, volume, avTransportURI)

You can read more in the README for each project.

To run it as a service under linux, I suggest using pm2 (https://github.com/Unitech/pm2). You need to run it in forked mode (-x)!

For Windows, you might try Winser http://jfromaniello.github.io/winser/

Cheers!
    This topic has been closed for further comments. You can use the search bar to find a similar topic, or create a new one by clicking Create Topic at the top of the page.

    391 replies

    jishiAuthor
    Headliner II
    July 2, 2014
    If you want to try out the mobile version, you can pull the "mobile" branch from sonos-web-controller, or download this:

    https://github.com/jishi/node-sonos-web-controller/archive/mobile.zip

    then visit http://IP:8080/m from your mobile browser. Only tested in Chrome for Android as of now. You can also try the "Add to homescreen" feature, which would open it up fullscreen.

    Today only prev, play/pause, next and group volume works. And it will control the first zone that it finds, no way to select.
    Trending Lyricist I
    August 1, 2014
    Thanks for your work. I've been using AirSonos for a couple weeks, and its awesome.
    jishiAuthor
    Headliner II
    August 2, 2014
    Thanks for your work. I've been using AirSonos for a couple weeks, and its awesome.

    Thanks, but I can't take credit for AirSonos, haven't been involved in that at all. Looks really neat though!
    Lyricist I
    August 7, 2014
    Just tested the tts functionality and as long as nothing is playing it works great. However the fact that the que is replaced is a bit of a problem. I have seen a few mentions of the ability to save the playstate and then reload it afterwards. Any pointers as to how I could achieve that?
    Contributor I
    August 8, 2014
    Hi,

    I've been working on a little project recently and I've been using the node-sonos-discovery module to handle Sonos events. (Thanks for creating it, BTW, it's fantastic!) However, I think I might have found a small bug ...

    During development, I stop/restart the discovery code many times, and it sometimes fails trying to subscribe to UPnP notifications. I see the following error in the console and then the discovery service just hangs:


    { [Error: connect ECONNREFUSED]
    code: 'ECONNREFUSED',
    errno: 'ECONNREFUSED',
    syscall: 'connect' }


    After poking about in the sonos.js code I discovered that it was trying to subscribe to events from my Phillips Hue Bridge! Here's an example of the response received:


    HTTP/1.1 200 OK
    CACHE-CONTROL: max-age=100
    EXT:
    LOCATION: http://192.168.0.108:80/description.xml
    SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1
    ST: upnp:rootdevice
    USN: uuid:2f402f80-da50-11e1-9b23-0017880a2371::upnp:rootdevice


    I might be wrong (I'm not experienced with UPnP), but I assume that the variable PLAYER_SEARCH that is broadcast is intended to filter so that only Sonos devices respond? Maybe this isn't working quite as intended.

    I've managed to work around this issue by hacking the code to check if the response contains the following string


    urn:schemas-upnp-org:device:ZonePlayer:1


    and ignoring any that don't. Not sure if this is the best solution though!

    Anyway, thanks again for a great module and I hope this feedback is helpful to you!
    jishiAuthor
    Headliner II
    August 9, 2014
    Hi,

    I've been working on a little project recently and I've been using the node-sonos-discovery module to handle Sonos events. (Thanks for creating it, BTW, it's fantastic!) However, I think I might have found a small bug ...

    During development, I stop/restart the discovery code many times, and it sometimes fails trying to subscribe to UPnP notifications. I see the following error in the console and then the discovery service just hangs:



    After poking about in the sonos.js code I discovered that it was trying to subscribe to events from my Phillips Hue Bridge! Here's an example of the response received:



    I might be wrong (I'm not experienced with UPnP), but I assume that the variable PLAYER_SEARCH that is broadcast is intended to filter so that only Sonos devices respond? Maybe this isn't working quite as intended.

    I've managed to work around this issue by hacking the code to check if the response contains the following string



    and ignoring any that don't. Not sure if this is the best solution though!

    Anyway, thanks again for a great module and I hope this feedback is helpful to you!


    You are correct in the assumption that I only scan for devices matching the correct ST, and this has always worked on my network. My best guess is that the Hue does not respect this and replies anyway, in violation to specs.

    With this in mind, it would be wise to check this on replying devices as well. If you send me a patch I will review it and merge it. Thanks for sharing the discovery, this might probably happen with a few other devices.
    Contributor I
    August 11, 2014
    You are correct in the assumption that I only scan for devices matching the correct ST, and this has always worked on my network. My best guess is that the Hue does not respect this and replies anyway, in violation to specs.

    With this in mind, it would be wise to check this on replying devices as well. If you send me a patch I will review it and merge it. Thanks for sharing the discovery, this might probably happen with a few other devices.


    Thanks for the response!

    I've forked the project, and I'm currently testing my patch in a branch.

    Once I'm happy, I'll submit a pull request for you to take a look at.
    Contributor I
    August 11, 2014
    OK - pull request sent!
    jishiAuthor
    Headliner II
    August 11, 2014
    OK - pull request sent!

    Thank you very much! Hopefully this resolves this issue for a few users.
    Contributor I
    August 11, 2014
    Thank you very much! Hopefully this resolves this issue for a few users.

    No problem - thanks again for a great module!

    I have a quick question, if that's OK? I notice that if I create a stereo pair from two speakers I get 4 topology-change events in quick succession (although not as a contiguous block - there are also some volume and transport-state events mixed in with the topology-change events.)

    The events seem to be identical, so I can't work out why there is more than one! For instance, grouping the speakers creates just a single topology-change event, as I would expect.

    It's not a big problem for me, as I can handle it by aggregating the events and treating them as a single topology-change. I was just wondering if this was something you'd experienced and if you knew why it happened?

    Thanks!