node.js - Using socket.io modules in other JS files -


the following app.js configured socket.io functionality, things working expected ,but want use socket functionality js file same project, how 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 want use socket.io module. if want create shared functionality, create module in app socket.io, ie.:

var io = require('socket.io')(server);  module.exports = function(options) {   // code here } 

if not answer question, please more specific.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -