denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.02k stars 5.23k forks source link

WebSocket: Ping and Pong #15430

Closed sinclairzx81 closed 5 hours ago

sinclairzx81 commented 2 years ago

Hi, Would it be possible to include the methods .ping() and .pong() on Deno's Web Socket implementation? It's noted that while browsers do not support sending these control frames, they can be a server side requirement if deploying WebSocket servers behind load balanced environments.

Infrastructure such as AWS's Application / Network Load Balancer (as well as many other load balancers) will automatically terminate idle connections. Applications can mitigate LB termination by periodically sending ping() control frames down to each connected client. Because ping frames are sent out of band from the sockets messaging channel, server applications can keep their connection keep alive logic and application protocol logic separate (with the added benefit of browser Web Sockets not seeing ping control frames at all)

Without this functionality, it does raise quite a few challenges porting existing application protocols to run on Deno. This is due to applications often omitting ping control frames in the applications protocol itself (as it's generally understood that these control frames are already provided as part of the RFC6455 specification)

RFC

https://www.rfc-editor.org/rfc/rfc6455#section-5.5.2 https://www.rfc-editor.org/rfc/rfc6455#section-5.5.3

Rust

https://docs.rs/ws/latest/ws/struct.Frame.html#method.ping https://docs.rs/ws/latest/ws/struct.Frame.html#method.pong

Node

https://github.com/websockets/ws/blob/master/lib/sender.js#L209 https://github.com/websockets/ws/blob/master/lib/sender.js#L252

Would it be possible to include these methods?

sinclairzx81 commented 2 years ago

Closed due to radio silence on this issue.

littledivy commented 2 years ago

Seems desirable. Reopening.

littledivy commented 2 years ago

Spec issue: https://github.com/whatwg/websockets/issues/10

alexgleason commented 3 months ago

I discovered that ping/pong frames are sent automatically thanks to: #13172

But I had to set { idleTimeout: 30 } on the call to Deno.upgradeWebSocket to prevent nginx from killing the connection.

lucacasonato commented 5 hours ago

I am going to close this as we automatically ping-pong now.