fastapi_websocket_pubsub
fastapi_websocket_pubsub copied to clipboard
Add unsubscribe method so client can unsubscribe from topics
Hi
My application is long running and the client will subscribe and unsubscribe to different topics during its lifetime. To this end this PR adds an unsubscribe method to go along with the subscribe method.
I figure that other users might also benefit from this addition, thus this PR.
The method returns True and logs a warning if a request is made to unsubscribe a topic which does not exist. This is in line with the behaviour of fastapi_websocket_pubsub.event_notifier.EventNotifier.unsubscribe which also returns True if a subscription is removed with a non-existent subscriber id.
I see that the current test cases are quite readable and appear like best-practice examples of how to use the library. I don't want to disturb that style with tedious tests. Do let me know if you think test cases are required.
Br Eskild
Hi @nneskildsf this looks good all in all, please add tests for the new flow.
Hi @nneskildsf this looks good all in all, please add tests for the new flow.
Thank you for the encouragement.
In order to implement a test, I had to implement the following in pub_sub_client.py:
- Post connection subscription
- Unsubscribe
My implementation maintains support for not awaiting calls to the subscribe method if it is made before a connection is established. It will still work if the call is awaited though. If the connection is established, then calls to subscribe and unsubscribe must be awaited and warnings will be presented by the event loop if they aren't.
Looking forward to feedback.
Br Eskild
Ping @orweis. Have a great day!
@danyi1212 will review
Ping @danyi1212. Have a great day!