r/reactjs • u/sh03-dev • Sep 20 '26
Discussion Poor man’s real time with Tanstack Query
Has anyone ever experimented with using Tanstack Query and then having an invalidation bus (SSE or websocket or streamed query) that sends to the client the list of endpoints that should be invalidated after a mutation? If so did it work for you? Were there challenges?
Thanks
32
Upvotes
31
u/canarydev Sep 20 '26
so we did this websockets and worked fine eventually. the pattern itself wasn't the hard part
the problem and grief we hit was query key hygiene. the predecessor / dev that set it up basically structure some keys too broadly, so invalidations matched way more queries than intended.
once socket events started firing frequently that turned into a lot of unnecessary refetching and rerenders.
so my big advice is to make sure your keys are narrow and consistent before wiring up a bus that might hit them 50x a minute. batching / debouncing invalidation also helps a lot.
another big one i think i'd do differently is send domain events/resources over the wire than actual endpoints. IE: incident.updated + an ID and let the client map those events to query keys. that keeps the server from having to know about your frontend cache structure
2
u/92smola Sep 20 '26
What is the use case? Are you sure you need bith caching and real time cache invalidation, does that apply everywhere or just for some resources
5
u/Merry-Lane Sep 20 '26
If the list is correct and doesn’t trigger refreshes too regularly, it should be okay.
1
u/illepic Sep 22 '26 edited Sep 22 '26
Using this heavily at https://mush.bike. It's a little thing I threw together so my mountain bike buddies and I could plan rides with shuttles and bike racks. There's a lot of realtime when you create an event and people join and add their bikes to racks on vehicles.
2
u/MehYam Sep 20 '26
I have a system like this (not Tanstack Query, but my own), and an invalidation step that returns only the documents changed since the last fetch. Homebrew cache invalidation, works really well.
3
u/[deleted] Sep 20 '26
[removed] — view removed comment
2
u/[deleted] Sep 20 '26
[removed] — view removed comment
2
u/[deleted] Sep 22 '26
[removed] — view removed comment