143
votes

I keep reading about ping/pong messages in websockets to keep the connection alive, but I'm not sure what they are. Is it a distinct frame type? (I don't see any methods on a javascript WebSocket object in chrome related to ping-pong). Or is it just a design pattern (e.g. I literally send "ping" or any other string to the server and have it respond). Is ping-pong at all related to continuation frames?

The reason I ask is I'm using a python framework that runs behind Mongrel2, so I'm wondering if there's a way to send Mongrel2 a specific ping/pong message that would tell it to keep the connection alive without my python app needing to worry about it. Analogous to a having a separate HTTP method for it, I guess. And I imagine a dedicated ping/pong message frame could be simpler (less load on server and network) than the string "ping", though that probably wouldn't matter too much.

EDIT: I just looked at RFC 6455 and it looks like Ping and Pong are definitely control frame types with their own opcodes. So how do I send a Ping frame from javascript in Chrome?

3
Just ping from the server. Everyone knows about the networking issue on non-standard ports, so they're starting to ping at regular short intervals. I guess you could ping a poorly written server, but it might not be too smart to do anything sensitive with them.user1382306
@user1382306 ping from server first will use mobile device battery use very fast. Ping from client can save device battery.bronze man
@user1382306 Not quite everyone! What is the networking issue on non-standard ports?HappyDog

3 Answers

130
votes

There is no Javascript API to send ping frames or receive pong frames. This is either supported by your browser, or not. There is also no API to enable, configure or detect whether the browser supports and is using ping/pong frames. There was discussion about creating a Javascript ping/pong API for this. There is a possibility that pings may be configurable/detectable in the future, but it is unlikely that Javascript will be able to directly send and receive ping/pong frames.

However, if you control both the client and server code, then you can easily add ping/pong support at a higher level. You will need some sort of message type header/metadata in your message if you don't have that already, but that's pretty simple. Unless you are planning on sending pings hundreds of times per second or have thousands of simultaneous clients, the overhead is going to be pretty minimal to do it yourself.

14
votes

Ping is meant to be sent only from server to client, and browser should answer as soon as possible with Pong OpCode, automatically. So you have not to worry about that on higher level.

Although that not all browsers support standard as they suppose to, they might have some differences in implementing such mechanism, and it might even means there is no Pong response functionality. But personally I am using Ping / Pong, and never saw client that does not implement this type of OpCode and automatic response on low level client side implementation.

4
votes

a possible solution in js

In case of the WebSocket server initiative disconnects the ws link after a few minutes there were no messages be sent between the server and client.

  1. the client end initiative sends a custom ping message, to keep alive by using the keepAlive function

  2. the server end will ignore the custom ping message and responds to a custom pong message

let timerId = 0; 

function keepAlive(timeout = 20000) { 
    if (webSocket.readyState == webSocket.OPEN) {  
        webSocket.send('');  
    }  
    timerId = setTimeout(keepAlive, timeout);  
}

function cancelKeepAlive() {  
    if (timerId) {  
        clearTimeout(timerId);  
    }  
}