fastapi_websocket_pubsub icon indicating copy to clipboard operation
fastapi_websocket_pubsub copied to clipboard

Add unsubscribe method so client can unsubscribe from topics

Open nneskildsf opened this issue 6 months ago • 5 comments

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

nneskildsf avatar May 15 '25 10:05 nneskildsf

Hi @nneskildsf this looks good all in all, please add tests for the new flow.

orweis avatar May 20 '25 13:05 orweis

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

nneskildsf avatar May 20 '25 20:05 nneskildsf

Ping @orweis. Have a great day!

nneskildsf avatar Jun 02 '25 05:06 nneskildsf

@danyi1212 will review

orweis avatar Jun 02 '25 05:06 orweis

Ping @danyi1212. Have a great day!

nneskildsf avatar Jun 17 '25 06:06 nneskildsf