How to change RTK Query useQuery hook's statuses (isError, isSuccess) when using websocket?

81 views Asked by At

I am implementing API management in my TS React application using RTK Query. To optimize performance and prevent network/console tab clutter from excessive HTTP requests, we have chosen to implement websockets (deepstream.io) for monitoring common backend statuses like server, database, and external services.

I successfully updated the cache using the streamed event/data provided by the backend, using both dispatch and updateCacheData from onCacheEntryAdded func. However, I'm facing an issue where the query statuses remain unchanged, even when I receive an error message like this: {error: "Server is down"}. I suspect I may have missed something in the documentation.

The current state of the query is:

 getServerStatus: builder.query<SuccessWithMessage, NoArg>({
      queryFn: () => ({data: {message: "Server is live"}}),
      providesTags: ["ServerStatus"],
      async onCacheEntryAdded(
        _,
        { cacheEntryRemoved, dispatch  }
      ) {
        
        deepstream.event.subscribe('server/status', (event) => {  
          void(dispatch(serverApi.util.upsertQueryData('getServerStatus', undefined, result)));
        });
        
        // cacheEntryRemoved will resolve when the cache subscription is no longer active
        await cacheEntryRemoved;
        
        // perform cleanup steps once the `cacheEntryRemoved` promise resolves
        deepstream.event.unsubscribe('server/status');
      },
    }),

And my follow up question would be that how to implement custom startedTimeStamp?

0

There are 0 answers