websockets
websockets copied to clipboard
Keep track of latency ping/pong
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?
Yes, this is a valid request. Do you have a suggestion for what an "easy to use API" would look like from your perspective?
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.
The simplest API possible would be websocket.latency
, returning the latency in seconds, measured during the last ping.
Related to #958.
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.
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
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.