Sonos with node.js, my attempt!


Userlevel 4
Badge +14
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.

397 replies

Userlevel 2
I will take a peek at how it looks / works on the BB Z10 and let you know.

Controller works on BB Z10 except for volume slider control
Hey J,

Was trying out the new build.... so I took a backup of the ubuntu vm.... then patched everything.... After clean restart... did a tar file for the current node_web_controller and the http-api....

So then I did the fresh grab from git.....

http-api is working, minus the presets, which could just be my coding of the file.... But I can control the volume, etc....

So I thought perfect, I went to the web controller, since I wanted to see what the new version was like..... Installed the new modules in a cleaned out directory (the old files had been deleted).... Then I started the server.js.....

The console window seemed to stop here for a while:

code:
triggering state volume Master Bedroom
triggering state volume Office
triggering state volume LittleRm
triggering state volume Living Room
triggering state volume Rec Room
trying to subscribe to topology 192.168.10.190
triggering state volume Kitchen
undefined 'is active, clearing'
setting timeout
undefined 'is active, clearing'
setting timeout
undefined 'is active, clearing'
setting timeout
undefined 'is active, clearing'
setting timeout
trying to subscribe to topology 192.168.10.194
undefined 'is active, clearing'
setting timeout
undefined 'is active, clearing'
setting timeout
stream end
stream end
stream end
stream end
stream end
stream end


But then it started to receive updates etc, when it was running.

But the web server never seems to respond to a request to load from Chrome or FF.... (30 and 25).

If I old the old untouched version of the code I got from Steffest (fork) back in July, it works from a web perspective.

Let me know what info you need.....

ubuntu 12.10 running in a vmware vm
node v 10.....

TIA,
Userlevel 4
Badge +14
Well, the output seems fine, ad if the browser doesn't get a response, there is probably something regarding how I serve static content.

I see one major difference between steffest fork and mine, and that is on row 14 in server.js:

code:

fileServer.serve(req, res);
}).resume();

vs

fileServer.serve(req, res);
});


I'm missing that resume() statement, which might act up on another platform (because in windows this is fine). It might even be a difference between node 0.8 and 0.10.

Could you try and add it manually and see if that works? I will fix it if that's the case.
Userlevel 4
Badge +14
Actually, I think it really should be there so I committed a fix. Pull or download it again and see if it works.
Will do....

In a little bit of time troubleshooting here this afternoon, I remembered I removed nvm (version manager) when I was trying to get the services working....

So I thought, could be a version 0.8 to 0.10 bug....

So I reinstalled nvm, and then ran this with v0.8 on the original code, and everything seems to work.

I'll change back up to the v0.10 through nvm, and try your fixed server.js, and let you know.

Thanks... for the quick turn around.
Userlevel 4
Badge +14
No problem, it's good to get it sorted out. I pushed an update which implements a working group volume now as well. It uses the scroll wheel in the same manner as the official client, but haven't fixed the click adjustment yet. You could adjust volume before, but it never reflected the volume on load, or when adjusted in another client. This do work now.

Soon all the basic control is in place, which will be a milestone 🙂
Cool....

git updates "instruction" reading is obviously going to be on the list...

I also got your suggested pm2 scheduler running, so the server.js can now start on startup.... Much easier to setup configure etc, over the forever, etc.... It's not perfect, I think I have to bounce the pm2 id's on reboot, but after that it has been seeming pretty stable.

So it's definitely coming together.

Nice job....
Userlevel 4
Badge +14
I implemented some group management (not sure if something is missing, have I missed something?).

I didn't do the Sonos way because I think it sucks, so I did a drag and drop solution instead. Drag the player name onto another zone to move it, or drop it outside all zones to detach it as a standalone player. What do you guys think about that? I just realized that it might be a problem of you have a lot of players which actually fills up the whole left column, but I will try and find a solution to that.

This will of course not work on a touch device, but the intended usage for this is a desktop (I will focus on a mobile version and a tablet version later). Just realize that a web controller would be useful for ChromeOS users as well.
Userlevel 4
Badge +14
Okay, did a basic implementation of favorite selection, double clicking the favorite will replace the queue (or start radio).

I also recorded a video for those of you who don't want to mess around with node and git to try it out, this is how it looks like:

http://www.youtube.com/watch?v=Ad80vH1igFU

Actually, I'm pretty impressed my self with how good it actually behaves in a web browser...
Userlevel 2
Starting to look very good!! I have been following the development closely. I currently have the nodejs webserver hosted on my NAS4Free box running freeBSD. All functions work nicely and so far it makes a very useable controller and is super quick. Keep up the good work. Works with most browsers even mobile devices are reasonably functional. Keep this up and we might actually have a workable controller for the Linux community finally. Thanks again. Any basic assistance or testing required, if I can help certainly let me know. Ynot.
Agreed.... I've got node running in a ubuntu vm, and hosting from there....

So far any testing from desktops and the netbook are working.... Haven't tried the ipad, or the "phablet", phones....

Definitely great work.....
Hey J,

Been running the controller through pm2 for just over an hour now....

I've noticed on the "pm2 list", that the memory in use seems to be "slowly" increasing from:

22.5 mb, up to currently 41.992 mb....

Not saying there's a memory leak, as I have no idea what exactly pm2 is reporting for memory utilization. And a lot of that time it would seem that the "page" is in the background browser window.

But I thought you should know, since it's maybe something related to the node 10.0.21 build I've got vs your 0.8.x....
Userlevel 4
Badge +14
I haven't really looked into the memory utilization yet, so any observations are of interest.

I don't know how aggressive the garbage collect in node is, and if you have a lot of free memory I wouldn't be surprised it the memory usage seems high. When you've had it running for a few days, it would be nice to hear what your consumption would be.
Userlevel 4
Badge +14
And a lot of that time it would seem that the "page" is in the background browser window.

Could you explain what you mean by that?
Userlevel 2
I can report after 36 hours of continuous run time node shows no significant change in memory utilization. Configuration is:
freeBSD 9.1
node - development 0.9.3
Web Clients include: Windows, Puppy linux, Ipad, Z10 and Android.

Thanks
Userlevel 4
Badge +14
Current status:

http://upload.grabbarna.se/files/sonos-web-controller-0.4.2.png, you can update it like usual (git pull, rm -r node_modules, npm install)

I have added functional queue listing, which means that it list the queue of the selected zone and you can double click in it to change track. It will lazy load the album art from the players (I use all players for this to speed things up), and then cache it locally for faster access next time. First time load can be a pain though, if you have a large queue.

It doesn't lazy load the queue, but load it in batches of 100 items. On a 3000 items queue this takes approx 30 seconds on my machine, but I think the bottleneck is the Sonos players.

I haven't yet optimized the loading behavior and it is kind of heavy on the browser atm, and it will not reflect changes to the queue yet.

Regarding memory foot print, it seems like there has been some changes between v 0.8 and 0.10. In 0.8 I rarely got over 30 MB, but in 0.10 I can easily use up towards 70 MB. Some says that they have introduced a less aggressive garbage collect, which might explain this. However, it would be nice to hear about your impressions on systems with less RAM, because hopefully it would be more aggressive when you have little free RAM left.
Heh, I still don't follow. What exactly was the problem? Didn't it update? Or did it update and you didn't expect it to?

No... it's was fine. Giving you context for the memory stuff. Didn't want you to think I was clicking around or modifying things. Just letting it do it's thing in the background. Just like to do with one of the desktop controllers....

Found after running 24+ hours, ram was between 35->50megs or so....

Bigger issue was a log file generated with pm2....

/root/.pm2/logs/[pm2-name]-out.log

Was 35ish meg.

tail/view the log file, it was all the info dumps server.js ... When you start in console window with node server.js, it dumps a running log right... well the -out.log file was basically those screen dumps put to log file. Longer, or more activity, the faster the growth in the file.

I'm sure there's a pm2 option I need to set to prune the logs after x. I just haven't found it in the documentation.
Userlevel 4
Badge +14
Seems like one doesn't always have an albumArtURI for the queue and favorites, i added a fallback for it. I don't think it has anything to do with socket.io, at least the error show no indication of that.

I also added dynamic favicon and title. Maybe it's just confusing if it keeps changing the favicon, however I thought it was nice :)

Anyway, I just pushed an update for sonos-discovery and the web controller, update as per usual. I have also lowered the amount of output generated from the process, especially from socket.io, which will greatly reduce the log file size.
Userlevel 2
Hey guys, great project - I'm looking forward to getting this up and running.

I've tried getting this up and running on my NAS and think I'm pretty close but may have possibly run into a bug.

I git cloned the latest web controller, ran npm install and started node server.js which seems to start fine, but once I browse to the website it half loads and crashes - it appears to be related to album art.

code:

# node server.js
scanning for players...
info - socket.io started
http server listening on port 8080
creating cache dir failed, this is probably normal { [Error: EEXIST, mkdir './cache'] errno: 47, code: 'EEXIST', path: './cache' }
trying to subscribe to topology 192.168.188.26
subscribing to events 192.168.188.25 Lounge Room RINCON_000E58B0B84E01400 600
subscribing to events 192.168.188.26 Bedroom RINCON_000E5880C3F401400 600
topology change emit
trying to subscribe to topology 192.168.188.25
triggering state volume Bedroom 6
calculating group volume Bedroom
Lounge Room "urn:schemas-upnp-org:service:ContentDirectory:1#Browse" STATUS: 200
triggering state volume Lounge Room 16
calculating group volume Lounge Room
trying to subscribe to topology 192.168.188.50
Bedroom "urn:schemas-upnp-org:service:ContentDirectory:1#Browse" STATUS: 200
Lounge Room "urn:schemas-upnp-org:service:ContentDirectory:1#Browse" STATUS: 200
fetching album art from 192.168.188.25
fetching album art from 192.168.188.26
fetching album art from 192.168.188.25
fetching album art from 192.168.188.26
404 'linking' 'L2dldGFhP3U9eC1maWxlLWNpZnMlM2ElMmYlMmZkcm9ibyUyZm11c2ljJTJmQXVkaWFibGUlMmZBdEhvbWVBU2hvcnRIaXN0b3J5b2ZQcml2YXRlTGlmZVVuYWJyaWRnZWRQYXJ0MV9tcDMzMl9kYW5pZWxzY2hlbGwuYWEmdj0yNzk='
serving /getaa?u=x-file-cifs%3a%2f%2fdrobo%2fmusic%2fAudiable%2fAtHomeAShortHistoryofPrivateLifeUnabridgedPart1_mp332_danielschell.aa&v=279

stream.js:81
throw er; // Unhandled stream error in pipe.
^
Error: ENAMETOOLONG, open './cache/L2dldGFhP3U9eC1maWxlLWNpZnMlM2ElMmYlMmZkcm9ibyUyZm11c2ljJTJmTWFqb3IlMjUyMExhemVyJTI1MjAtJTI1MjBGcmVlJTI1MjB0aGUlMjUyMFVuaXZlcnNlJTI1MjAoMjAxMyklMmYwMSUyNTIwWW91J3JlJTI1MjBObyUyNTIwR29vZCUyNTIwKGZlYXQlMjUyMFNhbnRpZ29sZCwlMjUyMFZ5YnolMjUyMEthcnRlbCwlMjUyMERhbmllbGxlJTI1MjBIYWltJTI1MjAlMjUyNiUyNTIwWWFzbWluKS5tcDMmdj0yOTA='
#


Screenshot of how much loads can be found here:
https://www.dropbox.com/s/f8bnu621qcevxbb/webcontrollercrash.png

Any suggests would be appreciated!
Userlevel 4
Badge +14
Hi, yeah got the same report from another user. I have pushed an update which will use a hash for filename instead, which will prevent the filename too long error.

run a git pull and try again. No changes to the dependencies so you don't need to remove the node_modules folder or run npm install this time.
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.
Userlevel 4
Badge +14
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.
Userlevel 4
Badge +14
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!
Userlevel 2
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.
Userlevel 4
Badge +14
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)