-
The server return an error type message to client, and I want to resubscribe upon error (websocket is not closed by server). How do I subscribe again? The major challenge is that in theory, there can be multiple subscriptions going on in the websocket, and the only identification I can get is Do I have to store |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Maybe I am not understanding your question, but why not literally re-subscribe on error? Roughly: import { createClient } from 'graphql-ws';
const client = createClient({
url: 'ws://unstable:4000/graphql',
});
(async () => {
for (;;) {
try {
await new Promise((resolve, reject) => {
client.subscribe(
{
query: 'subscription { iMightFail }',
},
{
next: (val) => {
console.log(val); // or emit the value however you want
},
error: reject,
complete: resolve,
},
);
});
// subscription has completed successfully, break the loop to avoid subscribing again
break;
} catch (err) {
// subscription received an error message, simply continuing the loop will resubscribe
continue;
}
}
})(); |
Beta Was this translation helpful? Give feedback.
Maybe I am not understanding your question, but why not literally re-subscribe on error? Roughly: