Webhook node server with socket.io
First of all: Nice work! I'm using your project in my LCARS-SONOS player (https://github.com/TimoKorinth/lcars-sonos) on a RaspberryPi with Windows 10 Iot and it works really well. I don't want to poll the server to get updated data, so I use your webhook to talk to another node server which uses socket.io to push the information to all clients. I created a small node server for this: https://github.com/TimoKorinth/sonos-push-server, but here's the main code:
var http = require('http');
var io = require('socket.io')(http);
let server = http.createServer((req, res) => {
req.on('data', (data) => {
io.emit('change', data.toString());
});
req.on('end', () => res.end());
});
io.on('connection', function (socket) {
console.log('A client is connected:' + socket);
});
server.listen(5007, function(){
console.log('listening on *:5007');
});
io.listen(5008, function(){
console.log('listening on *:5008');
});
Maybe you could integrate it in your sample server code?!
Are you only using this for pushing data over a websocket, or are you using the http-api for other purposes?
Because I already socket.io in the web-controller, which would provide you with events when something happens. It is also fairly easy to just implement sonos-discovery directly without using the webhook, since that would allow you to listen for all kinds of events. There is no problem running multiple instances on the same machine.
Maybe in the future it might make sense to incorporate a websocket push as well, but right now, the use case is fairly limited IMO, and it would give me more stuff to maintain (and I try to avoid that since I have very limited time).
Ah ok, I did not know this. So I could use your server.js (based on sonos-discovery) from your web-controller project and run it in parallel, right?! I use the http-api from my Angular2 client (https://github.com/TimoKorinth/lcars-sonos) to control my sonos players.
Yes, you could, but it might also be an idea to build your own socket.io service with sonos-discovery directly. It would be a fairly trivial service to build, without all the stuff for the web controller to run.
Or, you modify the web-controller and add the LCARS layout to it in the static folder (and access it from /lcars) and make use of the stuff that I have already done. However, I have used web-socket for everything, which doesn't really work well with Angular and it's digest cycle (at least not in angular 1.4), but maybe you can work around that. Would simplify the setup for you as well, no CORS requirement etc.
OK, thanks. Maybe I'll give it a try, but right now I'm happy with my solution. One of my main goal was to use Angular2 (and Windows UWP) for this. And you are right, I faced some CORS requirements ;-)
Changed the following things for this (maybe it's interesting too):
- Changed "sonos-http-api.js" in nodesonos/lib folder. Add the following lines to requestHandler function (just above "res.write(new Buffer(jsonResponse));"):
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.setHeader("Access-Control-Request-Method", "POST, GET, OPTIONS");
- res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, cache-control, pragma, expires");
- Change "req.method === 'GET'" in nodesonos/server.js to "req.method === 'GET' || req.method === 'OPTIONS'" (~line 66)
Yeah, we had a discussion about it in this issue: https://github.com/jishi/node-sonos-http-api/issues/47
But then it kind of fell of the grid... Should probably look into it again, your suggestions is a good start.
hi after upgrade to latest node,npm,sonos-http-api, and implementing Timo suggestion, I'm back at CORS problems:
code looks like this:
handleAction(opt, function actionCallback(response) {
if (response) {
var jsonResponse = JSON.stringify(response);
res.setHeader('Content-Length', Buffer.byteLength(jsonResponse));
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "POST, GET, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content$
res.write(new Buffer(jsonResponse));
}
res.end();
but getting error:
XMLHttpRequest cannot load http://192.168.0.33:5005/Bad/playpause. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
as always, the command is actually executed.
I have been running on old version of sonos/nodejs for months with this working:
handleAction(opt, function actionCallback(response) {
if (response) {
var jsonResponse = JSON.stringify(response);
// res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Length', Buffer.byteLength(jsonResponse));
res.setHeader('Content-Type', 'application/json;charset=utf8');
res.write(new Buffer(jsonResponse));
}
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
res.end();
but after upgrading, I got errormsg like: can't setHeader after write
I'm calling from a html/JS thing running locally on my PC.
So Timo: how are you using this, since it must be working for you ?
Try the beta, I think I have fixed all CORS issues in that one. Just checkout the beta branch instead of master.
thanx for really fast answer. And now I fell stupid, but how to take the beta ? Should I just append /beta on the URL to sonos http-api ? I can't test it right now, not near my computer, and I would love to have the answer, so I can test it in the weekend
Depends how you installed it to begin with. If you used git, just switch branch with:
git checkout beta
and follow the normal installation procedure (npm install --production).
If you downloaded the zip from github, just replace master.zip with beta.zip (https://github.com/jishi/node-sonos-http-api/archive/beta.zip), unzip and follow the normal installation procedure.
I actually just did npm install https://github.com/jishi/... but I will use one of your suggestions, probably the download via wget. Thanx again
no CORS problems using the beta, very nice