Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket - Should client send ping frames?

As the title suggests, should Ping Frames only be sent from a server or it is better to have both endpoints send them? As mentioned in the Websocket RFC:

NOTE: A Ping frame may serve either as a keepalive...

So by by having one endpoint sending a ping request it should keep the connection open, right?

The second part of above line is this:

or as a means to verify that the remote endpoint is still responsive.

I'm new to the concept of websockets but if the connection closes from the server won't the client be notified?

like image 297
Kim Byer Avatar asked Feb 03 '16 18:02

Kim Byer


People also ask

What is a ping frame WebSocket?

A ping or pong is just a regular frame, but it's a control frame. Pings have an opcode of 0x9 , and pongs have an opcode of 0xA . When you get a ping, send back a pong with the exact same Payload Data as the ping (for pings and pongs, the max payload length is 125).

Does WebSocket need heartbeat?

The presence or absence of a heartbeat indicates whether or not your WebSocket is functioning properly; it does not indicate the validity of a channel, in either direction.

Does WebSocket use keep alive?

Keepalive in websockets To avoid these problems, websockets runs a keepalive and heartbeat mechanism based on WebSocket Ping and Pong frames, which are designed for this purpose.

Can a WebSocket server send message to client?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired. This event acts as a client's ear to the server.


1 Answers

Consider the case where the server just goes away, maybe it crashes. Who or what will notify the client of this? Or say a network link close to the server is down for so long that by the time it comes back up, the server has totally forgotten about this client. Who or what would tell the client?

There are three possibilities:

  1. The client does not need to detect loss of the connection. In this case, there's nothing special you need to do.

  2. The client has some way to detect loss of the connection already. For example, if the connection is idle for some period of time, the client could send an application-level query and timeout if it gets no response or if the query fails.

  3. The client needs to detect loss of the connection but has no existing way to do this. In this case, using pings makes sense.

In a typical query/response protocol, the client usually doesn't need to ping the server because there's some query it can send that has the same effect. Unless the protocol layered above websocket supports some way for the server to query the client, the server often has only two choices: use pings to detect lost connections or disconnect idle clients.

like image 126
David Schwartz Avatar answered Oct 06 '22 08:10

David Schwartz