Simplifying Data Streaming (SSE, WS, FireStore) #7594
Unanswered
okalil
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've seen a lot of different solutions for real time data with react query, but looks like there's not a consensus about the best way to deal with it. I made a simplified solution that is intended to be data source agnostic and focused on native APIs, like async generators,
AbortSignal
and the newPromise.withResolvers
.I appreciate if you leave a feedback if you think this approach makes sense.
Why
Real time subcriptions allow us to keep data in sync, which is one of the main objectives of react query. Sounds like one overlaps the other, but I still think using both together is useful because of the great state management that react query provides. I can easily show loading and error UIs for initial load "without touch any global state" or dealing with
useState
anduseContext
. Besides, if the next update event fails, in some cases I can keep showing the latest cached version.How
Implementations
Sockets Live Data
FireStore Live Data
SSE Partial Updates
Notes
useEffect
to clean the subscription to avoid multiple effects when using custom hook in multiple places at the same time.Promise.withResolvers
is a new API, supported in most modern devices and browsers, but can be easily polyfilled if necessary.Some Related discussions:
#1061
#2621
#418
Beta Was this translation helpful? Give feedback.
All reactions