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

    Trending Lyricist I
    November 19, 2013
    Perfect timing... I was going to post an error for the "enametoolong"....

    Just got the new code, and it working now....

    I think I've found a different flavour of bug though.

    Are you grabbing just the art directly from the player? Or are you limiting it to say 300x300?

    I have a screen cap from chrome 31, where it's loading the art, but it's "full" size.... so it's layered through the queue list.
    jishiAuthor
    Headliner II
    November 19, 2013
    Perfect timing... I was going to post an error for the "enametoolong"....

    Just got the new code, and it working now....

    I think I've found a different flavour of bug though.

    Are you grabbing just the art directly from the player? Or are you limiting it to say 300x300?

    I have a screen cap from chrome 31, where it's loading the art, but it's "full" size.... so it's layered through the queue list.


    Yeah, I'm aware of that issue. I have that fixed so the maximum size will be 180px width or height, which fixes this.

    Oh, and the n -> ff is a rather clumsy bug introduced by me 🙂 I didn't thought the img src would expand to a complete url, so I just replace "on" with "off" to switch state. This of course creates a weird effect if your hostname includes sONos, which becomes sOFFos, heh. I'll try and fix this tonight, hopefully I will have the individual volume control in place then as well.
    jishiAuthor
    Headliner II
    November 19, 2013
    Alright guys, did push some fixes right now for everything reported. I still have some issues with random coverarts not loading, but that's minor.

    I also have indivual volume control working, and it behaves like the official controller (click once to show). I removed the mute button for now since it wasn't working anyway, that is a later problem.

    Enjoy!
    Contributor I
    November 20, 2013
    Ran for a few minutes last night, looks good. Found the volume control over the last release or two to be getting kinda funky. Response for volume control is slow and a bit less predictable. By that I mean, when I go to slide the volume up, it seems to go down a bit then up. The behaviour seems to be less predictable than a few releases ago. I will test further t o see if it's just me.

    Secondly, when you queue up a favorite playlist, the queue display is not updated. in order to update it, you need to refresh the web controller form the browser. This refresh should be automatic when queuing up a mew playlist.

    Other than that, initial test looks good. I will try more detailed testing over the next couple of days. Good work.
    jishiAuthor
    Headliner II
    November 20, 2013
    Yeah, currently known issues:

    Queue and favorites doesn't update when changes are made.

    Queue doesn't list correctly when having multiple zones.

    Volumes for all players are always shown, regardless of selected zone.

    group volume doesn't really work correctly when having multiple zones.

    Sonza favorites doesn't seem to work (maybe other services as well, please let me know)
    Lyricist III
    November 20, 2013
    First I am hacker not a programmer but i have manage to install your code and looking forward to learning from it but I have a few ?

    Could you provide a complete zip for the html and web interface as i keep getting init errors when running server will forward errors later

    it maybe that you are not supporting the older sonos units upon scanning zp80 and the zp100 as the code seems to die when it finds that unit


    Many thanks for all your amazing work .....I now have some more work to play with for my hobby

    Regards

    Robert
    jishiAuthor
    Headliner II
    November 20, 2013

    it maybe that you are not supporting the older sonos units upon scanning zp80 and the zp100 as the code seems to die when it finds that unit


    I don't have any old units my self so I haven't tested it. However, I can't think of anything that should misbehave just because of this. If you have any errors, please post them here and maybe it will give me a hint. In the meantime maybe someone else can confirm if old units work?
    buzz
    Grand Maestro
    November 20, 2013
    robertboca,

    I'm not using jishi's package, but I would not expect any model specific issues at the level jishi's program operates. Is there something different about the connection (Wired? Wireless?) to these two units?
    Lyricist III
    November 20, 2013
    yes one is wired and the wireless:

    C:\xampp\htdocs\sserver>node server.js
    http server listening on port 5005
    scanning for players...
    trying to subscribe to topology 192.168.1.209
    trying to subscribe to topology 192.168.1.111
    subscribing to events 192.168.1.111 Patio uuid:RINCON_000E5813DC2801400 120
    subscribing to events 192.168.1.209 Master Bedroom uuid:RINCON_000E5813DC7201400
    120
    subscribed to topology 192.168.1.111 412
    subscribed to topology 192.168.1.209 412
    trying to subscribe to topology 192.168.1.162
    type LastChange
    Volume
    subscribing to events 192.168.1.162 Living Room uuid:RINCON_000E5850693601400 12
    0
    type LastChange
    TransportState
    type LastChange
    TransportState
    trying to subscribe to topology 192.168.1.50
    type LastChange
    Volume
    type LastChange
    Volume

    events.js:72
    throw er; // Unhandled 'error' event
    ^
    Error: Unclosed root tag
    Line: 0
    Column: 1240
    Char:
    at error (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_modules\
    sax\lib\sax.js:616:8)
    at strictFail (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_mod
    ules\sax\lib\sax.js:636:22)
    at end (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_modules\sa
    x\lib\sax.js:623:27)
    at Object.SAXParser.end (C:\xampp\htdocs\sserver\node_modules\sonos-discover
    y\node_modules\sax\lib\sax.js:137:24)
    at SAXStream.end (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_
    modules\sax\lib\sax.js:209:16)
    at IncomingMessage.onend (_stream_readable.js:483:10)
    at IncomingMessage.g (events.js:175:14)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

    C:\xampp\htdocs\sserver>
    jishiAuthor
    Headliner II
    November 20, 2013
    Is one of them using fixed volume perhaps? Seems like it choke on the xml sent by a volume event. I should probably add an error handler for the xml parsing, since it fails occasionally for no apparent reason too.