Giter Club home page Giter Club logo

Comments (8)

horacimacias avatar horacimacias commented on August 14, 2024 4

maybe my following comment is picky and/or incorrect, feel free to ignore.

In my case I was starting with a minimum test application that was opening the websocket and not writing/reading from it. My server was tearing down the connection because of missed pings, which I understood Tungstenite would automatically and immediately handle.
That didn't seem to be the case.

Then I added code to read from websocket and reply to pings from my test application. Then I noticed on the server I had two replies to pings, 1 presumably generated by Tungstenite and the other one by my application explicitly.
I removed the explicit replies and any socket writing in my application and I still saw ping replies.

Long story short, here's my understanding so far. Please let me know if this is incorrect.

  • Does Tungstenite handle pings? If/when you read from the websocket, yes it will handle then and reply to them.
  • Do I need to do any websocket write for the ping to be handled/answered? No. as long as you read from the websocket, Tungstenite will write the response for you regardless of you ever using the websocket for writing or not.

...oh, and by Tungstenite I may well be mixing Tungstenite and Tokio above.
Just wanted to share my observations in case they are of any help.

from tokio-tungstenite.

sdroege avatar sdroege commented on August 14, 2024 3

You need to read from the read-side of the socket so that it receives/handles pings, and on the next write it would then send the corresponding pong.

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 14, 2024 2

This is documented and belongs to Tokio. Tungstenite does nothing unless it is called. There are no things like background threads. If you don't call any of Tunstenite APIs, it does nothing. It just won't be there.

In WebSocket you could receive any messages at any time. Not only pings. Your application has to either poll WebSocket periodically or start a background job waiting for messages in order to handle them. If you do anything like that, Tungstenite will handle pings for you.

from tokio-tungstenite.

horacimacias avatar horacimacias commented on August 14, 2024 1

thanks!

This is likely a silly question but is it possible that pings are only replied to if my client reads anything from the socket?

My sample client was made of 2 peers, one sender-only (websocket 1) and one receiver-only (websocket 2).
On my sender-only code I was not doing any read from the websocket and I see on the server there are no ping/pongs for that client.
I updated my code to read and send pings when received, as I thought this crate was not replying to pings, and on the server then I saw 2 pongs, likely the one generated internally and the one created by me.
Does this make sense?

from tokio-tungstenite.

kellytk avatar kellytk commented on August 14, 2024 1

You need to read from the read-side of the socket so that it receives/handles pings

Is that documented? As IMHO it contradicts:

Even in async mode Tungstenite replies to pings automatically and immediately

from tokio-tungstenite.

daniel-abramov avatar daniel-abramov commented on August 14, 2024 1

Does Tungstenite handle pings? If/when you read from the websocket, yes it will handle then and reply to them.

Yes, it does. But the reading part should make some progress, so that the underlying functions which read from the socket (and write the response) do some progress.

Do I need to do any websocket write for the ping to be handled/answered? No. as long as you read from the websocket, Tungstenite will write the response for you regardless of you ever using the websocket for writing or not.

Right (but only for this particular case). In case you want to write some other messages explicitly, you have to make sure that the corresponding tasks make some progress. Normally this is ensured in "typical" server applications, i.e. when you split reading and writing part and there are tasks which use them.

from tokio-tungstenite.

daniel-abramov avatar daniel-abramov commented on August 14, 2024 1

@horacimacias it seems like the original question has been answered, so I'll close the issue (it just easier to track what is still open when the closed issues are closed ;), but in case there are any issues, feel free to re-open this one or open a new one.

from tokio-tungstenite.

agalakhov avatar agalakhov commented on August 14, 2024

Yes, pings are supported. Even in async mode Tungstenite replies to pings automatically and immediately. Ping behavior is covered by standard Autobahn tests. The client you're looking for is autobahn-client from Tungstenite examples.
It is safe just to ignore pings as Tungstenite handles them for you.

from tokio-tungstenite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.