UseSubscription
and SubscribeToMore
both seem to take a subscription
query and return data through an updateQuery
callback.
I’ve been reading lots of web sites including the Apollo docs, but I can’t really see what the difference is yet.
What is the difference between UseSubscription
and SubscribeToMore
?
The main difference is that subscribeToMore
is tightly coupled with a query, while the useSubscription
hook stands on its own. You would use subscribeToMore
on a query result to keep that particular query result up to date in the cache when subscription data comes in. With useSubscription
, you are more flexible and can update whatever cache object you need.
I personally prefer useSubscription
. Most of the time, you want subscriptions to be active at all times, even when not browsing the particular page that uses the data it updates. A simple example would be a chat feature somewhere on your site. You wouldn’t want the subscription to be active only on the chat page, but everywhere else as well, because otherwise the application would be unaware of any new chat messages when not on the chat page. useSubscription
allows your subscription to be decoupled from your query, so that you can listen for new data wherever you need.
1 Like