blog/using-web-sockets-with-react-query #63
Replies: 20 comments 22 replies
-
Great article! In our company we use the same principle as described under "event-based-subscriptions". It works flawlessly and has several advantages over sending objects. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the useful article! In our project we create a single ton pool from the subscribed data and periodically call a force |
Beta Was this translation helpful? Give feedback.
-
In think throttling the websocket updates, storing intermediate results in a ref, might work. But I don’t think that the app should crash because of too many updates. Maybe you’re re-rendering the whole application every time instead of just the parts that are interested in the update? |
Beta Was this translation helpful? Give feedback.
-
What is the purpose and benefit of using React Query for web sockets? |
Beta Was this translation helpful? Give feedback.
-
@jayporta The purpose of websockets is to get live notifications of events happening on the server. The purpose of react-query is to manage async state in React. Queries and subscriptions are not the same, but they go well together. You can of course manage your subscriptions outside of React Query, but you'll likely be overpushing data, depending on your subscription model. You can also turn websockets on and off and the queries will still continue to work if the data lives in react-query. |
Beta Was this translation helpful? Give feedback.
-
why not using immutable updates of normalized data inside queryClient.setQueriesData method or even use immer, it would be way easier I certify |
Beta Was this translation helpful? Give feedback.
-
soooo nice! thanks you |
Beta Was this translation helpful? Give feedback.
-
You probably don't want to use it on websocket or any streaming. React Query is not meant to do this. Instead, make a global state and your initial value is coming from your react query, then update your state using simple old reducer. You dont want to keep your components rerendering when using |
Beta Was this translation helpful? Give feedback.
-
@TkDodo, where do you call the The article doesn't explain it and from merely reading it, it's not completely obvious because a hook call without storing anything in a variable is something I see rarely: function App() {
useReactQuerySubscription()
const { data, isLoading } = useQuery('todos', fetchTodos)
if (isLoading) return <div>loading..</div>
return data.map((todo) => <p>{todo.title}</p>)
} |
Beta Was this translation helpful? Give feedback.
-
I appreciate your all articles about RQ, awesome. About this post. You provide simple example, but can you tell me how would you handle dynamic data but in list with pagination? I am facing that case right now and its really difficult... |
Beta Was this translation helpful? Give feedback.
-
How do I actually access the data I have set with |
Beta Was this translation helpful? Give feedback.
-
Great articule with some Details , Thanks for sharing |
Beta Was this translation helpful? Give feedback.
-
Really appreciate the amazing content! Thank you. It'd be great if you can include an example of sending a payload from the client side as well. |
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
This is a great post... Thank you so much. I have been using websockets with redux and it was full of flaws. I ran into race conditions and useEffect-related trouble. |
Beta Was this translation helpful? Give feedback.
-
In react query, Is there a way to set isLoading state manually (using query key)? I am using websocket with react query. I found TkDodo's awesome blog on this I want to follow the approach in which I receive 2 things from backend in one WS message:-
But when using websocket I also want loading is fetching states. Since I am using websockets there is no Request promises i guess. Thus, I get no **isLoading, isFetching **states Is it possible to manually set these two states? My backend can send flags in each message to tell if data sending is complete or not.
|
Beta Was this translation helpful? Give feedback.
-
Thanks so much for this article, I'm just getting started with both Websockets and React Query myself (I really don't know why I left React Query for so long!) One thought that I've had, leading on from your last bit talking about const queryClient = new QueryClient();
const webSocketClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: Infinity
}
}
});
export default function App({ ...pageProps}) {
return (
<QueryClientProvider client={queryClient}>
<QueryClientProvider client={webSocketClient}>
<Component {...pageProps} />
</QueryClientProvider>
</QueryClientProvider>
)
} |
Beta Was this translation helpful? Give feedback.
-
I'm a bit stuck on trying to get this working. I've reread through this article but somehow I'm missing something because I can't seem to get the data I need back from the Web-Socket. Here's basically what I have.
|
Beta Was this translation helpful? Give feedback.
-
Thanks for article and all the amazing things you do. I was wondering if useMutation integrates with websockets? Can you use a mutation to send websocket messages? Will onSuccess, onError, etc. work? Do you have an example of this pattern? Thanks! |
Beta Was this translation helpful? Give feedback.
-
thanks for the article. Really broaden my understanding. Great work |
Beta Was this translation helpful? Give feedback.
-
Using WebSockets with React Query | TkDodo's blog
A step-by-step guide on how to make real-time notifications work with react-query
https://tkdodo.eu/blog/using-web-sockets-with-react-query?utterances=384634b31fbf3b2402057419SLRNNy81DMv4SwPhX1mtGHUUp5bKRxm4Fv8yoX%2B8%2BjOHOF3Gxii%2FaIyoCCfGvpa99Z4OrKXWTZBA6S7gdL9fkfrfoatqTppPuThtDYdjF7xQSRTVrUpOkNEGrg4%3D
Beta Was this translation helpful? Give feedback.
All reactions