How to separate websocket logic and app.js in Nodejs in app.js let app = express(); //.... //.... const http = require('http').createServer(app); require('./socket/index').listen(http); //... //... http.listen(port, () => console.log(`Server is listening on port: ${port}`)); in socket/index.js const socketIO = require('socket.io'); module.exports.listen = app => { let io = socketIO.listen(app); exports.sockets = io.sockets; io.on('connection', function(socket) { //... //... socket.on("eventOne",() => { // ... }) socket.on("eventTwo", () => { // ... }) } return io } wanna access from other files const {sockets} = require('../socket/index').sockets; sockets.sockets["testSocketID"].emit("testEvent", { message: "message body" });