Peerjs: Connection pinging

Created on 29 Mar 2015  路  11Comments  路  Source: peers/peerjs

I setup a peerjs server on Heroku. Heroku's connections timeout after 55 seconds, unless they are pinged. So we need a way to ping an established connection constantly, otherwise we have to reconnect after 55 secs. How to do this?

I also submitted this issue here: https://github.com/peers/peerjs-server/issues/68

Most helpful comment

I made it work like this:

function pingHeroku() {
    peerConnection.socket.send({type: 'ping'});
    timeoutID = setTimeout(pingHeroku, 20000);
}

However, accessing the current socket isn't anywhere in the docs.

All 11 comments

is this also happening on heroku paid servers ?
Otherwise you need to ping it with an external service such as pingdom, however it may violate terms of use on heroku free.

Well, paid or not paid is not the issue here. I'm actually pinging already, for other reasons. Heroku states, if you want to have a long running connection you need to ping it regularly. Cool would be, if peerjs had a method for that, like connection.ping. Even better if automatic pinging per connection could be configured, with an interval setting.

I made it work like this:

function pingHeroku() {
    peerConnection.socket.send({type: 'ping'});
    timeoutID = setTimeout(pingHeroku, 20000);
}

However, accessing the current socket isn't anywhere in the docs.

Oh, cool, interesting that works. I was trying something similar, but got errors from the peer server, as the ping message i sent was not recognized... So my plan was to modify peer-server.js. But now I will try your solution first. Thanks.

Okay, this seems to work, great. I assumed I have to do some sort of ping pong, but it seems enough to simply ping from client to server...

Although I do get ERROR PeerServer: Message unrecognized. Not overly clean, but it works :)

I might adjust the server code so that it doesn't throw that error...

Beautiful, thank you guys!

How exactly do i go about adding that code ?

This worked for me! Thanks so much!

can someone explain more because I'm not understood when should I have edited my code to add this?

HEARTBEAT message was added in 0.4.0

Was this page helpful?
0 / 5 - 0 ratings