Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
303 stars 81 forks source link

❓Recommended way for storing socket instance globally? #148

Closed Mohamed-kassim closed 2 years ago

Mohamed-kassim commented 2 years ago

Any recommended way of storing socket instance after creating for now I am storing it in global scope

  const socketClient = TcpSocket.createConnection(
    {
      port: parseInt(portNumber, 10),
      host: ipAddress,
      timeout: 10000,
    },
    () => {
      console.log('[SYNC] Client Connection Is Now initiated successfully');
    }
  );
  global.socketClient = socketClient;

And the same for the server instance so initiating the server in useEffect in the required part of the app and store instance in global then reusing it across the full app. it's known that it's not recommended to save values in global, also bad for typescript in react native so any other recommended way, as is it safe to store it in a global state like zustand? or some opinions mentioned initializing outside components and exporting instance and then importing it from file and use it.

Rapsssito commented 2 years ago

@Mohamed-kassim, I am sorry, but this kinds of questions should be asked on stackoverflow. I will be closing this issue.