Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send a ping opcode using javascript Websocket?

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

like image 744
Nahum Bazes Avatar asked Aug 16 '26 01:08

Nahum Bazes


1 Answers

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.

like image 74
wntwrk Avatar answered Aug 17 '26 14:08

wntwrk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!