Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must websockets have heartbeats?

Tags:

When I read about websockets, heartbeats are usually mentioned as a must have. MDN even writes about a special opcode for heartbeats.

But are heartbeats a mandatory part of websockets? Do I have to implement it or else my websockets will be terminated by the browsers or some other standards?

like image 649
Holger L Avatar asked Sep 08 '17 08:09

Holger L


1 Answers

The RFC 6455, the current reference for the WebSocket protocol, defines some control frames to communicate state about the WebSocket:

  • Close: 0x8
  • Ping: 0x9
  • Pong: 0xA

Ping and Pong are used for heartbeat and allows you to check if the client is still responsive. See the quote below:

A Ping frame may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive.

But when the client gets a Ping, a Pong must be sent back to the server. See the quote:

Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame. It SHOULD respond with Pong frame as soon as is practical.

Bottom line

When designing both client and server, supporting heartbeats is up to you. But if you need to check if the connection is still alive, Ping and Pong frames are the standard way to do it.

Just keep in mind that if a Ping is sent and a Pong is not sent back, one peer may assume that the other peer is not alive anymore.

like image 196
cassiomolin Avatar answered Sep 18 '22 19:09

cassiomolin