I think your on the right track, you could read through https://tkdodo.eu/blog/using-web-sockets-with-react-query and see if you could improve it. Probably I would just set up the central useEffect subscription to invalidate the queries we need instead of faffing about with the functions in appState.