Closed rhishikeshj closed 5 months ago
Hi @rhishikeshj - thanks for the question. There's definitely options for doing this, but rather than explaining them - would it be easier for you if Client::Subscribe
took a &self
rather than &mut self
?
I was a bit unsure about my decision to keep it as &mut self
and think it'd be easy to change if it's causing issues.
Although actually, I suppose that would still give you problems calling close
because you need an owned client for that....
@rhishikeshj I think https://github.com/obmarg/graphql-ws-client/pull/88 should make things much eaiser for you - do you want to take a look and try it out?
@obmarg Thank you for being so responsive to my question! I will definitely take a look at #88 and come back to you tomorrow morning.
@obmarg Yup, the changes in #88 work well. Now I can just clone the client and send it to multiple threads and also close the connection when the work is done. Thanks again for making the change quickly! If it helps, I can contribute a multi-threaded multi subscription example to the repo too :)
Looking forward to using this in the next release version.
Hi, I am looking for some help with a specific usage pattern for
graphql-ws-client
. I couldn't find the solution in the examples or on the interwebs.I ll try and describe the setup with some psuedo-code.
I have the use-case of subscribing to 5 different queries. From the
cynic-multiple-subscriptions
example, it is recommended that I re-use theClient
struct and callclient.subscribe
with as many queries as I have. And then loop over the returnedStream
In my case, the job of subscribing and then looping over the potentially never-ending stream is done on separate threads for each query, and the updates are passed through mpsc channels.
I also have a requirement of being able to close all subscriptions when a certain event occurs. For this I looked at the
Client::close
function. Now the trouble is that since I need to pass in the graphql client value to multiple threads, I decided to wrap that in anArc<RwLock<Client>>
RwLock because the subscribe method needs a mutable self so...I also changed up the
subscribe_to*
functions to take an Arc, acquire a write lock before callingsubscribe
The issue is, when I need to close the client, I need an inner owned value to pass to theclose
function. So I tried something likeArc::into_inner(arc_rwlock_graphql_client)
But since every
subscribe_to
clones the Arc, theinto_inner
will always returnNone
because that's the requirement of theArc::into_inner
fnSo my question is, what is the recommended way to implement such a pattern? Where I have to subscribe to multiple queries from multiple threads and also retain the ability to close all of them at once.