The following is my app.js that I configured for socket.io functionality, The things are working as expected ,but now i want to use this socket functionality another js file from the same project, How to achieve this?
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Just write
var io = require('socket.io')(server);
in each file where you want to usesocket.io
module. If you want to create shared functionality, create another module in your app withsocket.io
, ie.:If its not the answer for your question, please be more specific.