Li Haoyi
Li Haoyi

Reputation: 15812

Websockets: what to do about lost Pings?

I'm trying to implement a protocol to match-up the received Pongs with the corresponding Pings.

But what do I do about the Pings I send but don't receive a Pong? Could this happen? Or is it guaranteed that all pings will have corresponding Pongs, and that never receiving a Pong indicates loss of connectivity? The websocket protocol document is silent with regard to this.

Upvotes: 1

Views: 880

Answers (2)

zaphoyd
zaphoyd

Reputation: 2760

A conforming WebSocket implimentation must return a pong upon recieving a ping. In general, no pong after a reasonable amount of time means either a network error or something else is busy or broken on the remote endpoint. There are a few caveats.

  • Only the most recent ping must be acknowledged. If you send two pings you might only get one pong back.
  • ping and pong are control frames. WebSocket does not have a separate channel for control frames. A large data frame may delay the receipt of a ping that immediately follows it. Implementations may fragment data messages to improve control message latency.
  • pongs will not be sent after a close frame has been recieved.

Upvotes: 2

Philipp
Philipp

Reputation: 69703

Websockets are based on the TCP protocol which ensures that all packets are processed in-order and that lost or damaged packets are retransmitted before continuing to process the incomming data.

So the only possible explanation for a missing pong is an error in the implementation of the ping/pong protocol on the other side of the connection.

The reliablility of the TCP protocol and its ordering can easily be seen while webbrowsing. When you have a bad internet connection, websites loading might get interrupted while in progress, but those parts which do load are always in the right order and without any errohEThgf%$/GHfDw(&IjfdsAyW$§REggnfdER§$HHxM+FD5

Upvotes: 2

Related Questions