DEV Community

loading...

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

Collapse
blouzada profile image
Bruno Louzada

@bravemaster619 I'm trying to use this strategy with a complex react app, with authentication, tests, I think i solved authentication but the tests are impossible, did you used this with a complex app or just for simple apps? can you help me?

Collapse
bravemaster619 profile image
bravemaster619 Author • 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
Forem Open with the Forem app