0
votes

I'm trying to export socket.io server through multiple node script so i can emit notification on the same port.

Here is my main server.js file code:

var express = require('express'),
app = module.exports.app = express();

const options = {};

var server = http.createServer(app);
var io = require('socket.io').listen(server);
exports.io = io;

server.listen(3000, function()  {
    console.log('Node.js Global app is running...');
});

Below is other node script are runninig when i try to require server.js i get this error:

Error: listen EADDRINUSE 0.0.0.0:3000

server_tn.js

var express = require('express'),
    app = module.exports.app = express();

var code_pays = path.basename(__dirname);
console.log('Node.js app is running...' + code_pays);

var main = require('./../main.js');
var importIo = require('./../server');
var io = importIo.io;

main.mainTraitement(code_pays);
1
You are exporting a net server, not an instance of net server. You probably wants to share an instance.. By the way, you should use request to reach your net instance, this is the most re-usable way I think. - Izio
Is it possible to export an instance of net server ? - Haithem Rihane
What about search before opening useless thread? stackoverflow.com/a/28051798/8126612 - Izio
the issue is when i require socket.io using var io = require('socket.io')(3000); i get the error Error: listen EADDRINUSE :::3000 - Haithem Rihane

1 Answers

0
votes

You can't have more than one program listenning to a specific port. Check if you have any program listening on port 3000, or if on your main.js you are also listenning on port 3000.