Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS, socketIO, multiple files

I'm a little bit confused;

I would like to use socketIO on NodeJS app. I've created this (pseudo)code:

//server.js
var app = express();
//some code...
var router = require('./app/router');
app.use(router);

var server = app.listen(appConfig.app.port, function () {
    var port = server.address().port;
});
var io = require('socket.io')(server);
io.on('connection', function (client) {
    console.log('Client connected...');

    client.on('join', function (data) {
        console.log(data);
    });
});


//client.js
var socket = io.connect('http://localhost:5555');
socket.on('connect', function(data) {
    socket.emit('join', 'Hello World from client');
});

Everything is fine. But !

At now, I would like to emit event in another file. I have router and POST request. I want to emit event on POST request (request handler is in another file).

//router.js
router.route("/addmenu").post(function (req, res) {
        menuModel.addMenu(req.body,function(data){
            //I WANT EMIT HERE
            res.json(data)
        });
    };
);

I have to initialize router before start server, but I have to pass server to IO... How pass IO to router ?

like image 672
IceManSpy Avatar asked Apr 17 '16 12:04

IceManSpy


People also ask

How many rooms can Socket.IO handle?

socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.

How many players can Socket.IO handle?

Socket.io maxes out my CPU at around 3000 concurrent users. This is on Intel i7 CPU. Because of this I have to run multiple node/socket.io processes to handle the load. For 100 concurrent connections you should be fine.

Does Socket.IO use Websockets?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.

Is Socket.IO a WebRTC?

Socket.IO P2P provides an easy and reliable way to setup a WebRTC connection between peers and communicate using the socket. io-protocol. Socket.IO is used to transport signaling data and as a fallback for clients where the WebRTC PeerConnection is not supported.


2 Answers

You can try this

//server.js
var app = express();
//some code...
var io;
var getIOInstance = function(){
  return io;
};
var router = require('./app/router')(getIOInstance);
app.use(router);

var server = app.listen(appConfig.app.port, function () {
    var port = server.address().port;
});

io = require('socket.io')(server);
io.on('connection', function (client) {
    console.log('Client connected...');

    client.on('join', function (data) {
        console.log(data);
    });
});

//router.js
module.exports = function(getIOInstance){
    router.route("/addmenu").post(function (req, res) {
        menuModel.addMenu(req.body,function(data){
            //I WANT EMIT HERE
            getIOInstance().sockets.emit(...)
            res.json(data)
        });
    };
    return router;
);

This solution will work if you want to 'notify' all connected clients.

If you need to notify only a specific client, then I will advise you to use an event-emitter module in order to communicate these events and not share your socket instances across multiple files.

like image 71
jahnestacado Avatar answered Sep 17 '22 23:09

jahnestacado


In router.js you can do something like:

//router.js
module.exports = function(io) {
var router = //What you declared it to be
router.route("/addmenu").post(function (req, res) {
    menuModel.addMenu(req.body,function(data){
        //I WANT EMIT HERE
        res.json(data)
    });
};
);
return router;
}


 //server.js
 //Change this line to be like the one below
var router = require('./app/router');
//.........
//.......

//Desired way
var router = require('./app/router')(io);
like image 22
ashr Avatar answered Sep 20 '22 23:09

ashr