Skip to content

Clarification/Feature Request: WebSocket lifecycle management in RTK Query streaming endpoints #4962

@jordan-mathews

Description

@jordan-mathews

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 HTTP
  • onCacheEntryAdded → 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions