Socket.io: is there a way to listen for the heartbeat?

Created on 9 Mar 2013  路  15Comments  路  Source: socketio/socket.io

is there a way to listen for the heartbeat event?

i would like to do a store.session.touch(); during this event

Most helpful comment

If you have the socket connection you can get the heartbeat event by:

socketio.on('connection', function(socket) {
  socket.conn.on('heartbeat', function() {
      console.log('heartbeat');
    });
});

But as above I don't know if this is the official way to do it!

Note this is for socket.io 1.0.x.

All 15 comments

There should be one. +1

+1

+1

Alot of +1's I think this should be implemented in the next release. I am willing to make a pull request if someone can guide me slightly on what files i should be looking at.

var handler = socket.manager.transports[socket.id].onMessage;
socket.manager.transports[socket.id].onMessage = function (packet) {
 handler.apply(this, arguments);

 if (packet.type === 'heartbeat') doStuff(); 
}

I don't want to sound like a dick here but have you guys even taken the time to look at the source code? This is an open source project, there's no magic, unicorns, compiled coffeescript garbage or secrets. All the answers you guys are searching for is just in the source. Instead of typing +1 just read the source. Exposing a heartbeat interface that emits an event every single time is just pointless overhead for everybody that's not interested in it.

P.S. I had to search just as hard as everybody else to supply you with that snippet above. Srly, try it click on a source file or just search the source for heartbeat and track it back from there.

@3rd-Eden
You didn't wanted to sound like a dick but you pretty much did. The thing is, guys who write the architecture of something are way ahead of people looking at source of something and can easily point out that you should read this file or something of this sort, plus its always better to know if its going to be merged in the official version of something. Nobody likes to waste his time and everybody who wrote +1 shows that he is willing to support such an addition/modification. Pasting a chunk of code from the source without telling where it is located and what dependencies it holds is of no use to anyone. Anyway, a reply from someone who knew more about the architecture would have been much better. I'll do the required modifications and waste my time drilling down into the source as this project already seems to be going ahead adding engine.io, etc which is not bad but going forward without looking at what needs to be done, to already existing features and how they can be improved is usually of higher priority.

.. you know also during the heartbeat it would be nice to get current headers of that connection also...

sometimes authorization is good. untill you add cookies or remove cookies.
and if a auth cookie is removed it would be nice to disconnect mabe or emit a event

 socket.on('heartbeat', function(data,next){
    data.headers;
    data.cookies;
    next();
});

@3rd-Eden think about this... currently i use my own socket heartbeat event... witch is way more overhead then if it was built in..

first thing i have to write client side..

socket.on("heartbeat",function(){
    setTimeout(function(){
        socket.emit("heartbeat")
    },15000);
})
socket.emit("heartbeat")

and then the server

socket.on("heartbeat",function(){
    socket.emit("heartbeat")
})

as you see the client has to start this loop. and its not logical for server to have interval. witch is bad because it create a extra connection of data to send and back and forth..

if the heartbeat on server just emits on a connection then it would solve a lot of people problems without using above example

I think this issue is about the client (socket.io-client), not the server.

At least with version 1.0.4 you can do this:

socket.io.engine.on('heartbeat', function() {
    // whatever
});

Note that you have to use socket.io.engine rather than socket.

I have no idea if this is supposed to be the official way though because I don't see any documentation for it. So +1. ;)

If you have the socket connection you can get the heartbeat event by:

socketio.on('connection', function(socket) {
  socket.conn.on('heartbeat', function() {
      console.log('heartbeat');
    });
});

But as above I don't know if this is the official way to do it!

Note this is for socket.io 1.0.x.

@cableman Thanks, this solved an hours worth of headache trying to find out how it's done now!

people warned me about socket.io ... why didn't i listen. stuck with no way to listen to pings on the server side. using v. 1.4.5.

@ForgeableSum please see the answer above

version": "0.9.16" what is the event to catch? is it socket.on('heartbeat') or socket.io.engine.on('heartbeat') ? anyone aware of it.

@shamun it should be the latter

Was this page helpful?
0 / 5 - 0 ratings