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
Post a Comment