-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Summary
As far as I can tell, RTK Query doesn’t currently provide a way to reflect disconnection or failure of a long-lived data stream (like a WebSocket) in the useQuery hook after queryFn has fulfilled successfully. As a result, isSuccess remains true even if the WebSocket fails to connect or closes unexpectedly. I’m looking for clarification or guidance on how to track and represent WebSocket open, close and error states with RTK Query.
Problem
If I understand the docs correctly, RTK Query recommends:
queryFn→ for initial snapshot via HTTPonCacheEntryAdded→ for subscribing to real-time updates (e.g. WebSocket)
This works well initially — but it’s unclear how to handle the case where the WebSocket fails to connect or is closed unexpectedly by the server. The result is that useQuery() continues reporting isSuccess: true, even though the real-time stream is broken, leading to misleading UI state.
Current Workaround
The only solution I’ve found is to manage WebSocket connection state separately (e.g., in a Redux slice), and expose that to components alongside the useQuery() result. This works, but feels like duplicating effort, especially since useQuery already has isLoading, isSuccess, and isError built-in.
Ideally onCacheEntryAdded would expose some kind of callback for updating these states.
Question
Is there a recommended way to reflect WebSocketopen, close and error in the useQuery state — for example, by setting isError: true or updating the error field after the stream fails?