5

I have a websocket client and I want it to send a ping frame to my WS server. According to the RFC 6455 a ping frame is represented by opcode %x9 but I don't know if it's even possible to send from a browser.

Any help will be appreciated

Nahum Bazes
  • 610
  • 1
  • 6
  • 15
  • They are automatically sent by the browser/server, you don't have to care about them yourself. There are no functions to force a ping or pong. If you want to, you need to hand-craft them in a WS packet structure. But there is no need to do so in JavaScript. A workaround is to just send an empty packet to the server but I don't see this is necessary either. – Daniel W. Jan 31 '22 at 13:35
  • 4
    Does this answer your question? [Sending websocket ping/pong frame from browser](https://stackoverflow.com/questions/10585355/sending-websocket-ping-pong-frame-from-browser) – Daniel W. Jan 31 '22 at 13:39
  • Thanks @DanielW., the project I'm currently working on requires to initiate a ping-pong from the client using the standard ping frame – Nahum Bazes Jan 31 '22 at 14:07
  • This is how the `ws` Websocket lib does it: https://github.com/websockets/ws/blob/8a7016dc2fe4d9d63c428c67588d7c1f33a72e5c/lib/sender.js#L209 – Daniel W. Jan 31 '22 at 14:12
  • Very interesting but can this be done on the client? – Nahum Bazes Jan 31 '22 at 15:16
  • Seems like a duplicate of @DanielW.'s link – Josh Feb 08 '22 at 22:16

1 Answers1

1

Building on-top of the comments in your original post, you can manually send pings via websocket.

The link by Daniel W. of the ws library has a ping() method which can be called from a valid websocket client. See an example here.

If you have written your own websocket library then you must conform to the standard framing outlined in RFC6455 The Websocket Protocol.

You should be able to pack a buffer with the correct header and control opcode and send it over the upgraded HTTP1.1 TCP connection.

Edit:
Further research shows, at the point of writing, the Ping method is not supported via the native Websocket Client and the send method seems to only support payloads and not command messages, which include opcodes for commands such as ping.

An option could be to go with a WASM based solution which could make non-XHR or non-fetch based TCP connections, however this comes with a whole different set of challenges.

wntwrk
  • 317
  • 1
  • 2
  • 10
  • 1
    This is not what I asked. I wanted to know if it's possible to programmatically initiate a ping frame (0x9) from a browser client. The example you provided does nothing to do with that. – Nahum Bazes Feb 10 '22 at 11:08
  • Apologises if there is a misunderstanding, perhaps you can give further information on what you mean by "programmatically initiate". In the link provided the ws.ping() method sends a ping message from a client, which can be done programmatically. This assumes you are using this library but after reviewing the source code, it looks like they are just packing a buffer and sending with the correct headers over the pre-initiated and upgraded TCP connection. – wntwrk Feb 11 '22 at 10:00
  • thanks. The problem is I don’t think this could run from a browser. If possible, can you add a working sample that will work in the browser? – Nahum Bazes Feb 12 '22 at 17:05
  • 1
    I've updated my answer, sadly client-side ping messages don't seem to be supported natively. – wntwrk Feb 13 '22 at 22:36
  • I see, thank you! – Nahum Bazes Feb 14 '22 at 07:01