-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
onSubscriptionData not triggering when extending useSubscription to handle multiple subscription (with different remote urls) #10908
Comments
Phew. You would have to change a lot more than these parts of Is there an option to call |
@phryneas The problem is the wss clients are dynamic. Those URL endpoints are fetched beforehand and the Apollo client is created. So I need to loop through useSubscription hook which react does not recommend. I also tried that because the length is fixed so the hook rules will not be broken. That results to the same problem as mentioned above. |
And your usecase is essentially that you have a number of wss clients and you want to merge their results together? In that case, I would suggest writing a custom Link for that instead 🤔 |
I created custom link to know which request goes through which wss url. Is that what you meant? I don't need to merge data as of now, just need to get the data from those different remote wss endpoints. This is the Link:
|
I was thinking something along the lines of (untested) const linksBySubscribeId: { [id: string]: ApolloLink } = {};
const httpLink = new HttpLink({
uri: httpUri,
credentials: "include",
headers: {},
});
export class SelectingLink extends ApolloLink {
public request(
operation: Operation,
forward: NextLink
): Observable<FetchResult> {
const subscribeId = operation.getContext().subscribeId;
let link = !subscribeId ? httpLink : linksBySubscribeId[subscribeId];
if (!link) {
link = linksBySubscribeId[subscribeId] = new WebSocketLink({
uri: `ws://localhost:4000/subscriptions/${subscribeId}`,
options: {
timeout: 5000,
reconnect: true,
},
});
}
return link.request(operation, forward);
}
} Of course the lazy link instantiation is optional ;) |
@phryneas Thanks for the response. |
Ah! My assumption after reading your initial code was that there was a problem where probably only |
I guess I got you confused a little 😀
As a wrapper while setting |
Issue Description
React hook implementation: https://github.com/apollographql/apollo-client/blob/main/src/react/hooks/useSubscription.ts from official doc.
I tried using the same implementation but with multiple subscription for multiple remote urls at once.
Subscription handler
Setting the subscription
Subscribing logic
The remote urls are set using the Apollo Link (distinguished as per the subscribeId sent in context) This case lets say I have 3 clients as:
Now if I subscribe to the same subscription using the multiple subscription hook implemented above:
All of the 3 clients are subscribing to the subscription query as expected. But the problem is it cannot trigger the onSubscriptionData method when data is being sent from server through websocket.
This might be related to #10693 .
Another issue I encountered here is we need to pass extra variables to be able to initiate same subscription endpoint to multiple remote wss urls
This is related to this: #10215 . Seems like that fix was reverted.
Link to Reproduction
n/a
Reproduction Steps
No response
The text was updated successfully, but these errors were encountered: