-
-
Notifications
You must be signed in to change notification settings - Fork 540
How to query the websocket state #100
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
Comments
The example in the documentation consist of a one message exchange session. You want to keep the connection alive. This means that both the server and the client handler coroutines should have an infinite loop. An extreme simple server side handler for your problem should look like this:
And the respective client side handler:
Of course the handlers should be made more sophisticated to deal with errors and exceptions, but I believe you can start with them. |
Another thing you can do is:
Really your question seems to be about orchestrating asyncio tasks (which has a learning curve). It doesn't look specific to websockets. I hope this helps! |
@SzieberthAdam Thanks. That looks good. But how do I create the second periodic, timer triggered event handler that logs my alive message in the client? More importantly, the alive message should only be logged if the websocket connection is still connected. Thus, I need to have a reference to the websocket... any suggestions? PS: I don't need the server in my actual code, because I am listening for updates from an external system, but it is good to have for testing puposes. |
@aaugustin Thank you for this as well. I haven't tried this, but it looks to me as if this is only polling for a single message. In my example, I am listening for randomly incoming messages from some IoT backend end. Hence, there can be many more than one per 300s. However, every 300s I want to log to a file a statement that tells me that my websocket is still connected. If it matters, I forward the logs to AWS CloudWatch where I have defined an alarm event to send me an email if ever the websocket did not log an alive statement within the 300s interval. |
Again, the questions you're asking have little to do with websockets. It's a matter of running two asyncio tasks, one that deals with websockets messages and one that checks regularly when the last message was received and sends an alert if it's too old. The two tasks can communicate through a global variable, to keep things simple. Really you need to learn about asyncio tasks. |
@dschien You can achieve this by separating the tasks of doing the communication and watching the connection. The three main
Please note that the code is not tested, I wrote it out of my head, so there might be errors in it. Take Aymeric's advice, and dig into asyncio documentation. It is a complex framework which is hard to pick up well. |
Thank you both. @aaugustin is absolutely right, as my opening line also said my question wasn't specific to this websockets but about asyncio. Thanks for both of your time. For those who might come across thread looking for a running solution, I ended up with a stackoverflow question here: http://stackoverflow.com/questions/35529754/python-async-websocket-client-with-async-timer. That works quite nicely. |
Hi,
I am new to async etc and before I file a stackoverflow ticket I thought I'd ask directly...
I need to monitor a long running client. My approach is to log an alive message and create an alarm if the message is absent.
My idea was to start with the sample client in the RTD getting started section (https://websockets.readthedocs.org/en/stable/intro.html) and add a second async message that logs a message all 5 minutes or so as long as the connection is alive.
I am sure what I am asking for is trivial. I just can't figure it out.
Many thanks
The text was updated successfully, but these errors were encountered: