Websocket - Should client send ping frames?
Asked Answered
D

2

7

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?

Donielle answered 3/2, 2016 at 18:32 Comment(4)
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?Diahann
I see.. so a client should ping the server.. thanks David :)Donielle
Unless it has some other way to detect loss of the connection. For example, it could detect that the server hasn't pinged it. Or it might be sending queries periodically already and could detect loss of connection by timing those out.Diahann
thanks for your quick response.. I've used websocket in node and it hid a lot of low level stuf >.< I'll be posting another question about Close Frame.. it's a very interesting subjectDonielle
D
5

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.

Diahann answered 3/2, 2016 at 19:18 Comment(0)
C
0

Both variants has ways of implementation. For example in case if server sends ping to client, then client can get information that server disconnected by have a loop with deadline timer which is reset every time when ping is received. If the timer reaches dealine, then it's mean that server disconnected.

Clegg answered 13/2, 2022 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.