I'm building an app that allows for watching multiple gaming streams at the same time. I want the client to tell the node server when it starts watching a stream and when the client stops watching that stream. That way, I can keep track of how long a user has watched a specific stream and add that stream to their viewing history as well.
My question is: how would I go about doing this? I was thinking of setting up keep-alive pings and once I find that the last one was a reasonable amount of time since the previous, the client is assumed to have stopped watching a stream. This seems like a bit of a hassle to set up properly. I want to know if there's a library that would allow me to do that easily. I've looked at socket.io, but I don't really understand the purpose of it or if it would apply to my situation.
Thanks in advance!
You can use socket.io to do this. There is built-in heartbeat funcionality in socket.io. What you need to do is handling disconnected event. More info at: https://github.com/LearnBoost/socket.io-spec
Update:
On your client:
socket.emit('startWatch', { streamid: 'stream1' });
....
socket.emit('stopWatch', {});
On your server:
io.sockets.on('connection', function (socket) {
// listen to startWatch event
socket.on('startWatch', function (msg) {
// handle startWatch here
});
//listen to stopWatch event
var stopWatchHandler = function () {... }
socket.on('stopWatch", stopWatchHandler);
socket.on('disconnected", stopWatchHandler);
});
Socket.io (or the like) is what you want.
io.sockets.on('connection', function (socket) {
console.log('user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
});