N
N
Nwton2016-05-18 17:03:27
Node.js
Nwton, 2016-05-18 17:03:27

How to connect socket.io?

In the app.js file (the main application file), I start listening to io and http.
There is a separate X module, which I included via require.
How to make io.emit from X?
That is, how to properly move io into a separate module, and then connect it to app and X
At the moment, app looks like this:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var x = require('./x');

app.get('/', function(req, res){
  res.sendFile(path.join(__dirname + '/public/index.html'));
});

http.listen(80, function(){
  console.log('http.listen on 80');
});

I tried module.exports.io = io in app and require(app) in X. But somehow it turns out strange, because I start X in app, and then I get app in X. In the console, a message about the start of listening is displayed 2 times. somehow not nice

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-05-18
@Nwton

var x = require('./x')(io);
// x.js
module.exports = function(io){
  io.emit(......);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question