Auto-reconnecting perpetual SSE #1022
-
|
I'm looking to have a stream that always tries to reconnect to the server no matter what. An example of this could be to display a live currency exchange rate, that will have new data forever. The current docs says: import { consumeEventIterator } from '@orpc/client'
const cancel = consumeEventIterator(client.streaming(undefined, {context: {retry: Infinity}}), {
onEvent: (event) => {
console.log(event.message)
},
onError: (error) => {
console.error(error)
},
onSuccess: (value) => {
console.log(value)
},
onFinish: (state) => {
console.log(state)
},
})But what about if the server instance shuts down gracefully? It should ideally try to reconnect (maybe with a backoff). Maybe something like this? function perpetual (){
const cancel = consumeEventIterator(client.streaming(undefined, {context: {retry: Infinity}}), {
onEvent: (event) => {
console.log(event.message)
},
onError: (error) => {
console.error(error)
},
onSuccess: (value) => {
console.log(value)
},
onFinish: (state) => {
perpetual()
},
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
orpc's Client Retry Plugin does support infinite retries for SSE streams by setting However, if the server shuts down gracefully and the stream ends without an error (i.e., the iterator completes normally), the retry plugin will not reconnect—the stream is considered finished. In this case, your recursive approach using For robust perpetual SSE, use the retry plugin for error-based reconnections and handle normal completion in Let me know if you want a concrete example of a safe perpetual loop pattern! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
After looking at the different options, here's what we landed on: const ac = new AbortController();
while (!ac.signal.aborted) {
const iterator = orpc.eventStream(undefined, {
signal: ac.signal,
context: { retry: Infinity },
}); // can also wrap the `for... await` in a try / catch instead using the ClientRetryPlugin
for await (const { event, data } of await iterator) {
// do stuff with events here
}
// potentially add a wait here between retries
} |
Beta Was this translation helpful? Give feedback.
orpc's Client Retry Plugin does support infinite retries for SSE streams by setting
context.retry: Infinity, and you can configure the delay between retries withretryDelay(default is 2000ms, or you can set your own). The retry logic will attempt to reconnect whenever an error occurs, including network failures and most server-side disconnects, and you can further control retry conditions with a customshouldRetryfunction. When the stream ends (either by error or completion), the plugin cleans up resources and stops retrying if the abort signal is triggered. This means you generally don't need to manually recurse with yourperpetual()pattern for error cases—retries are handled for you…