I have a nodejs backend and a react frontend. On nodejs I have socketio set up, on the client side I have the connection in the App component, and passed down using context. Now, using hooks, I can use the io variable globally and listen on client-side to events.
However, on server side, I have this IO variable also globally available, but how to use this on multiple pages? I have this code in server.ts:
const io = //initialize io io.on('connection', (socket: any) => { console.log('a user connected', socket.handshake.query); socket.on('disconnect', () => { console.log('user disconnected'); }); io.on('test', console.log); }); But, in a different file, I have to initialize my connection using io.on()again. How to work around this? I have a file called foo.controller.ts which has some actions around foo. In this file I want to listen all events related to foo.