Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of latency ping/pong #1195

Closed
Serpens66 opened this issue Jul 17, 2022 · 7 comments
Closed

Keep track of latency ping/pong #1195

Serpens66 opened this issue Jul 17, 2022 · 7 comments

Comments

@Serpens66
Copy link

Serpens66 commented Jul 17, 2022

Hello,
thanks for your work ;)
I'm using websockets as a client and would like to keep an eye on latency. Your documentation says this:
https://websockets.readthedocs.io/en/stable/topics/timeouts.html?highlight=timeout#latency-issues
So clients should directly call "await websocket.ping()".

But if ping_intervall is not None, websockets is already doing pings all the time. So why doesn't it track the latency already? Then there would be no need to make additional pings to get the current latency.

I'm just an amateur programmer and I tried to overwrite the "async def ping" function to keep track of latency, but your module structure seems to be really complicated (a connect function somehow starts "WebSocketClientProtocol" but only if awaited ... this is too advanced for me to understand so I don't know how to overwrite the ping function properly)

Another reason for me to "hack" into the "def ping" is ratelimiting. Some servers only allow up to x messages per y seconds. And therefore I of course need to be aware when a ping was made, since this reduces the number of other messages I can send.

-->
I think adding optional latency tracking is a good idea in general.
And maybe you can add some kind of easy to use API to get notified about pings, so I can note them regarding ratelimiting and maybe even also await the pong message.

Of course efficiency is more important than those features, so if the tradeoff is not good, can you tell me how to overwrite the ping function properly?

@aaugustin
Copy link
Member

Yes, this is a valid request. Do you have a suggestion for what an "easy to use API" would look like from your perspective?

@Serpens66
Copy link
Author

Yes, this is a valid request. Do you have a suggestion for what an "easy to use API" would look like from your perspective?

thanks :)
I'm not familiar what the common way for this is in python... do you have multiple ideas? Maybe I could say something to those ideas wether they would help for my request. I fear everything that I would come up with would be too bad coding.

@aaugustin
Copy link
Member

The simplest API possible would be websocket.latency, returning the latency in seconds, measured during the last ping.

@aaugustin
Copy link
Member

Related to #958.

@Serpens66
Copy link
Author

Serpens66 commented Jul 18, 2022

Yes, sounds good.
..
An advanced way for more customization, but only if this does not cost too much performance and is easy to implement for you:
I previously used "ws4py" threaded client. And there it was normal to create my own websocket class inheriting from the ws4py class and then overwrite some functions like "opened, closed received_message".
I thought about a similar way for your ping function. So lets call a new function "ping_feedback" and it will get eg. the pong_waiter object. In your code it will be an empty function doing nothing. But users could overwrite it and are this way able to get notified about everytime the ping function is called and they even get the pong_waiter object if they want to measure stuff themself.
..
But of course websockets is differently structured than ws4py, so I don't know if this is possible. But this is how I imagine it.

@aaugustin aaugustin changed the title [Feature Request] Keep track of latency ping/pong Keep track of latency ping/pong Jul 24, 2022
@aaugustin
Copy link
Member

This will be available in the next release.

Please find the answer to your last question here: https://websockets.readthedocs.io/en/latest/faq/misc.html#are-there-onopen-onmessage-onerror-and-onclose-callbacks

@roguh
Copy link

roguh commented Mar 10, 2023

I encountered a similar problem and I instead overrode the WebsocketProtocol class by passing the create_protocol argument to serve.

create_protocol (Optional[Callable[[Any], WebSocketServerProtocol]]) – factory for the asyncio.Protocol managing the connection; defaults to WebSocketServerProtocol; may be set to a wrapper or a subclass to customize connection handling.

I passed a subclass of the WebsocketProtocol as this keyword argument. My subclass kept track of additional data.

In your case, you could inherit from one of the Protocol classes to react whenever you read or write a frame, or when reading or writing a PING/PONG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants