31

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?

Holger Ludvigsen
  • 2,320
  • 1
  • 26
  • 31
  • 1
    Websockets are used very liberally and so the concept of a heartbeat or ping/pong is more of a suggestion for best practice rather than anything mandatory. – mnewelski Sep 08 '17 at 08:13

3 Answers3

27

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.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
9

It is mandatory or not depending on client and server implementations. If you are connected to a server that requires you to answer the PING with a PONG, you will be probably disconnected in case you don't reply. Same if you are the server and a client is sending you PING.

Server and client implementations vary (there are a myriad of them), but the browser´s javascript client do not send PING, and do not provide any API to do so, although It replies to PINGs with PONGs.

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • upvoted! a small doubt here, i am doing a websocket client on node to connect with bitfinex and binance, bitfinex sends heartbeat messages but binance doesnt, should I ping them regardless of whether they send a heartbeat message or should I only ping them every 30 seconds if they dont send heartbeat messages? – PirateApp Oct 04 '18 at 13:46
5

Pings and Pongs are not mandatory. They are useful, since they allow the detection of dropped connections. (Without some traffic on the wire, there is no way to detect a dropped connection.)

Note that in the browser, WebSocket heartbeats are not accessible. If you require your browser client code to detect dropped connections, then you have to implement hearbeating on the application level.

gzost
  • 2,375
  • 1
  • 18
  • 25
  • "Note that in the browser, WebSocket heartbeats are not accessible." - what do you mean? maybe **pings** are not accessible? or you are just saying they are not automatic? – Alexei Sosin Aug 09 '23 at 14:42