here is my code
export const streamingApis = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
keepUnusedDataFor: 10,
tagTypes: ["Message"],
endpoints: (build) => ({
streamMessages: build.query<any, string>({
queryFn: () => ({ data: [] }),
async onCacheEntryAdded(
args,
{ updateCachedData, cacheDataLoaded, cacheEntryRemoved }
) {
const ws = new WebSocket(`${process.env.API}${args}`);
if (args) {
await cacheDataLoaded;
// populate the array with messages as they are received from the websocket
ws.addEventListener("message", (event) => {
updateCachedData(() => {
return JSON.parse(event.data);
});
});
await cacheEntryRemoved;
ws.close();
}
},
}),
}),
});
export const { useStreamMessagesQuery } = streamingApis;
so Im using this snippet of code to open several webscoket connection with different args
here is how im using it
const { data: allTickerStream } = useStreamMessagesQuery(
WS_END_POINTS.ALL_MARKET_TICKERS_STREAMS)
or
const { data: marketStream } = useStreamMessagesQuery(
selectedSymbol.toLowerCase() + WS_END_POINTS.INDIVIDUAL_MARKET_STREAM
);
it will open multiple webscoket connection at same time and data keep coming, but I need to close some of my webscoket connection dynamically once a condition happen, please advise
thanks in advanced