DEV Community


Discussion on: How to prevent multiple socket connections and events in React

blouzada profile image
Bruno Louzada • Edited

I implemented socket io as middleware, my problem with socket in component and tests is mocking socket io with jest.mock, I dont want to connect to real server in tests, but with middleware approach I could make tests and it seems better to not depend on component life cycle to handle events;
I had a really hard time configuring websocket with React, with middleware approach I could met all requirements

  • 1 connection per client
  • Connect based on users only after login
  • Tests
  • Update application state
  • Show snackbars
  • Change page based on events
Forem Open with the Forem app