I need to emit an event to room clients from typegraphql resolver. I tried to modules.export = io
in index.js, but it creates a new instance, not the same. And, in result, it doesn't work
More likely you need to share a bit more code to know for sure , but this is something I did very often, something like :
// io.index.js
// setup io first
const io = require("socket.io")();
// or
const { Server } = require("socket.io");
const io = new Server();
// then export it
module.exports = io;
then just use it in another file like
// someotherfile.js
const io = require('./io.index.js');
io.emit("an event sent to all connected clients");
// or
io.to("room").emit("an event sent to all connected clients in room");
This way it's always the same instance of io.