0
votes

I'm implementing a WebSocket node.js server which has browser websockets as clients.

I want to implement a ping/pong mechanism, where each 5 seconds the server sends a Ping frame to the client.

So my questions are:

  1. what data payload should I use in the Ping frame?

  2. what is the recommended interval to send the Ping frames?

  3. can the browser initiate a ping request to my server, so I would need to implement a reply to a Ping event?

1

1 Answers

1
votes
  1. use whatever payload you want. WebSocket is just a transport, you decide what data it transports. Per the WebSocket spec:

    A Ping frame MAY include "Application data".

    Which means it does not have to send any data at all.

  2. use whatever interval is reasonable for the communications you are implementing with WebSockets.

  3. WebSockets are bi-directional, so a browser could send a Ping frame to your server. Per the spec:

    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. Pong frames are discussed in Section 5.5.3.

    An endpoint MAY send a Ping frame any time after the connection is established and before the connection is closed.

    Which means either party may send a Ping at any time.