DEV Community

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

Collapse
 
bravemaster619 profile image
bravemaster619 • Edited

Sorry for late reply. I see no reason why tests are impossible in this approach. Can you show me some of your code?

Collapse
 
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