How can i export socket io into other modules in nodejs

0 votes

I have socket.io working in app.js but when i am trying to call it from other modules its not creating io.connection not sure ?

app.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var ditconsumer = require('./app/consumers/ditconsumer');
ditconsumer.start(io);
server.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

consumer.js

module.exports = {
    start: function (io) {
        consumer.on('message', function (message) {
            logger.log('info', message.value);
            io.on('connection', function (socket) {
                socket.on('message', function(message) {
                    socket.emit('ditConsumer',message.value);
                    console.log('from console',message.value);
                });
            });
        });
}
}
Jun 17 in Node-js by Vaani
• 7,020 points
20 views

1 answer to this question.

0 votes

Because app.js is usually the main initialization module in your project, it will normally both initialise the web server and socket.io, as well as load other components that the app requires.

As a result, giving io to other modules in the function Object() { [native code] } function of that module is a common approach to distribute it. This is how it would work:

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

// load consumer.js and pass it the socket.io object
require('./consumer.js')(io);

// other app.js code follows

Then, in consumer.js:

// define constructor function that gets `io` send to it
module.exports = function(io) {
    io.on('connection', function(socket) {
        socket.on('message', function(message) {
            logger.log('info',message.value);
            socket.emit('ditConsumer',message.value);
            console.log('from console',message.value);
        });
    });
};

Or, if you want to use a .start() method to initialize things, you can do the same thing with that (minor differences):

// app.js
var server = require('http').createServer(app);
var io = require('socket.io')(server);

// load consumer.js and pass it the socket.io object
var consumer = require('./consumer.js');
consumer.start(io);

// other app.js code follows

And the start method in consumer.js

// consumer.js
// define start method that gets `io` send to it
module.exports = {
    start: function(io) {
        io.on('connection', function(socket) {
            socket.on('message', function(message) {
                logger.log('info',message.value);
                socket.emit('ditConsumer',message.value);
                console.log('from console',message.value);
            });
        });
    };
}

The "push" module of resource sharing is what it's called. By passing it in the function Object() { [native code] }, the module that is loading you pushes some shared information to you.

There are also "pull" models, in which a module calls a method in another module to get the shared information (in this case the io object).

Often, either model can be made to work, but depending on how modules are loaded, who has the needed information, and how modules will be reused in other situations, one or the other will feel more natural.

answered Jun 17 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How do I “include” functions from my other files in nodejs?

Hello @kartik, You require any js file,so you just ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,720 points
1,478 views
0 votes
0 answers

How can I completely uninstall nodejs, npm and node in Ubuntu

I installed it using: sudo apt-get install ...READ MORE

May 15 in Node-js by Kichu
• 19,040 points
172 views
0 votes
1 answer

How can i get the extension of the image in node.js?

Hello @kar You can do the following to ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,720 points
871 views
0 votes
1 answer

How can i update multiple documents in mongoose?

Hello @kartik, Use: Device.updateMany({}, { cid: '' }); Hope it ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,720 points
2,163 views
0 votes
0 answers

How to transfer data from a controller to a socket.io in nodejs?

I'm developing a web application that will ...READ MORE

5 days ago in Node-js by Vaani
• 7,020 points
12 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
967 views
0 votes
1 answer
0 votes
1 answer

How can I use goto in Javascript?

This JavaScript preprocessing tool allows you to ...READ MORE

answered Jun 9 in Node-js by Neha
• 8,560 points
22 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP