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

    Lyricist II
    January 29, 2014
    Jishi,
    first of all - a great piece of work.

    I have spent the whole day installing it (both web controller and API) on my RASPI and in the final step come across a problem when trying to run it in the background.

    I have tried using both FOREVER and PM2 to start the processes during boot time. This it does quite happily with no errors shown in the log files.

    The problem that I observe however is that the web client wont display in a browser if the process is started with FOREVER (or PM2 when the -x option is used).

    PM2 works if the mode is cluster - however this then eats 100% CPU.

    The log file contents are identical when starting in the background or when starting from a terminal window. It indicates no errors or warnings.

    The browser doesn't complain that the website is not there - it just sits and waits and displays nothing.

    Anyone have any ideas?

    I'm using the latest versions of everything (node 0.10.2) and the sonos-web-controller_0.6.0_pi build.

    Peter
    jishiAuthor
    Headliner II
    January 29, 2014
    Jishi,
    first of all - a great piece of work.

    I have spent the whole day installing it (both web controller and API) on my RASPI and in the final step come across a problem when trying to run it in the background.

    I have tried using both FOREVER and PM2 to start the processes during boot time. This it does quite happily with no errors shown in the log files.

    The problem that I observe however is that the web client wont display in a browser if the process is started with FOREVER (or PM2 when the -x option is used).

    PM2 works if the mode is cluster - however this then eats 100% CPU.

    The log file contents are identical when starting in the background or when starting from a terminal window. It indicates no errors or warnings.

    The browser doesn't complain that the website is not there - it just sits and waits and displays nothing.

    Anyone have any ideas?

    I'm using the latest versions of everything (node 0.10.2) and the sonos-web-controller_0.6.0_pi build.

    Peter


    Hm, that is odd. I just tried it out and it works on my raspberry. I do however run it as root, which you might not.

    What does the output from "netstat -ntpl" say? What OS are you running? Rasbian? Which port are you trying to run it under?

    And, does it work, if you run it directly from the shell? Even as the same user that pm2 will run under?
    Lyricist II
    January 30, 2014
    so everything is running as root (I'm lazy 🙂 )

    If I do not use PM2 or FOREVER and run directly from the terminal window in x-windows then it works.

    Run under PM2 from the terminal window in clustered mode it works.

    Run under PM2 from the terminal window in forked mode it doesn't work.

    The api program works without problem. It's just the web controller that has issues. The web browser seems to be connecting but just not receiving data (it doesn't even timeout??) have tried with Chrome and Firefox.
    Have even tried with only starting the web controller and leaving the api down - but I really want to use both.

    OS is RASPIDAN

    Port settings are all the defaults - I have not changed any of your settings


    UPDATE::::::
    Tried it again this morning and PM2 works!!!!
    FOREVER still doesn't work but if PM2 is working I'm happy. The only thing that I can think of that I had changed in the mean time was to put the node and application paths into the PATH statement in the etc/.profile
    jishiAuthor
    Headliner II
    January 30, 2014
    Make sure that it is running in forked mode. If you started under cluster mode, then stopped it, and started it again with -x (without deleting it from pm2) it will still start in cluster mode.

    Also, did you fix the startup script for pm2? (pm2 startup) That should fix environment variables, and then you should start pm2 using the init.d script.
    Lyricist II
    January 30, 2014
    Yep did all that :)

    As I said, suddenly started working this morning.
    jishiAuthor
    Headliner II
    January 30, 2014
    One theory is that you started it in cluster mode once, which would have made the pm2 daemon hog the relevant ports. When you later on tried in forked mode or forever, it wouldn't bind to them. It should have raised an error though...
    Lyricist III
    February 3, 2014
    Hi there :

    Do you think if i merged both lib http and web to one server there would be a conflict ?

    Many Thanks

    Regards

    Robert
    jishiAuthor
    Headliner II
    February 3, 2014
    Hi there :

    Do you think if i merged both lib http and web to one server there would be a conflict ?

    Many Thanks

    Regards

    Robert


    No, that is actually preferred. Use a single instance of SonosDiscovery. I do that my self.
    Lyricist III
    February 3, 2014
    Could you upload your version with it combined I am also using the remote control code. I keep blowing it up for some reason ........

    :~(
    Thank You
    Robert
    jishiAuthor
    Headliner II
    February 4, 2014
    Could you upload your version with it combined I am also using the remote control code. I keep blowing it up for some reason ........

    :~(
    Thank You
    Robert


    I don't run the IR remote and the web controller together, since they aren't modularized. Adding the http api is no problem, but I need to rework some stuff to make it easier to package it all together. Right now I'm doing a rewrite of all XML parsing to speed things up, but the plan is to create a complete distribution which contains all parts, which you can configure on/off instead. That would probably make it easier to install.