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, 2022 in Node-js by Vaani
• 7,070 points
4,289 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.

To know more about Node JS, It's recommended to join Node JS Certification Course today.

answered Jun 17, 2022 by Neha
• 9,020 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,840 points
3,428 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, 2022 in Node-js by Kichu
• 19,040 points
2,424 views
0 votes
0 answers

How can i download high quality you tube video using ytdl-core package in nodejs?

my code snippet as below: const express = ...READ MORE

Aug 19, 2022 in Node-js by Neha
• 9,020 points
1,708 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,840 points
2,094 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

Jun 23, 2022 in Node-js by Vaani
• 7,070 points
2,678 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
1,927 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,455 views
0 votes
1 answer

How can I use goto in Javascript?

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

answered Jun 9, 2022 in Node-js by Neha
• 9,020 points
4,392 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,876 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP