RTK Query: Design pattern for socket connection on login, then passed to API middleware? #1857
-
I'm really liking RTK Query and how it's helped me simplify data handling and CRUD. I'd like to ask about design patterns around streaming updates. I'd like to use a middleware similar to what markerikson did here to create a socket connection on login and enable my React app to emit updates to my database, and then having listeners in the API per the docs. However, I can't create multiple connections - there needs to be only one socket connection, one associated with a logged-in user, and which the app can use elsewhere to emit messages. phryneas's example shows an instance where the socket connection is created when the API is created. Is there a way to pass a socket connection created through middleware to the api middleware? I suppose that would mean socket middleware dynamically updating the api middleware. I probably need to do more digging into createApi, but wondering if anyone can give pointers in the direction of solutions or alternatives. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The simplest option would probably just be to keep a reference to the socket instance in a module file somewhere, with getters and setters if needed, and use that in both places. |
Beta Was this translation helpful? Give feedback.
The simplest option would probably just be to keep a reference to the socket instance in a module file somewhere, with getters and setters if needed, and use that in both places.