snapview/tungstenite-rs

ping/pong docs

Closed this issue · 1 comments

Akuli commented

When I started using tokio-tungstenite, here's everything I did:

  • Figure out how to accept a connection
  • Figure out how to receive a message
  • Figure out how to send a message
  • Read docs of the Message enum
  • Write code
  • Test code with chromium
  • Prevent out-of-memory attacks with an appropriate WebSocketConfig

Here's how I handled Ping: every time my code gets a Ping, it puts the ping data to a tokio::sync::mpsc channel, where a different part of my code will pick it up and send the corresponding Pong. I'm not familiar with details of the websocket protocol in general, apart from how it's initially just HTTP, but I'm familiar with a few other protocols where ping/pong works this way.

But turns out I shouldn't do any of that, as this library automatically responds to pings. I didn't catch this when testing, because chromium doesn't send websocket pings. I also didn't find this in the docs (although I didn't search much). I just happened to look at the issue tracker for another unrelated problem, and that's how I discovered this.

It would be nice if the automatic responding was documented in the Message enum docs. It should also mention that you can still receive Pings to add your custom logic in addition to the automatic response.

The automatic replies to ping messages (automatic queueing pong messages) are documented in read_message() and write_message() which are the primary functions to work with the WebSocket 😉

Also, the fact that a user can still receive Ping messages is part of the Message enum definition that the read_message() returns. If messages cannot be received, they would not be a part of the enum for apparent reasons (an exception is the Message::Frame that we recently added and explicitly documented in the enum variant).

Hope this helps!