Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i export socket.io into other modules in nodejs?

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);                 });             });         }); } } 
like image 965
hussain Avatar asked Jul 21 '16 18:07

hussain


People also ask

How do I integrate Socket.IO in node JS?

To get started with developing using the Socket.IO, you need to have Node and npm (node package manager) installed. If you do not have these, head over to Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal.

Can you use Socket.IO without node JS?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes.

Is Socket.IO a library or framework?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.

What can I export from Node JS?

Node.js already exports in-built modules which include fs, path, and http to name a few. But you can create your own modules. Node.js treats each file in a Node project as a module that can export values and functions from the file. Say, for example, that you have a utility file utility.js with the following code:

How do I import a socket server in Node JS?

And then in your Server.js, you should import Socket.js like this: import app from './app'; import SocketServer from './socket'; const server = app.listen (process.env.PORT || 3333, '0.0.0.0'); const Socket = new SocketServer (server); export default Socket;

What are ES modules in Node JS?

If you want to learn more about ES Modules (along with CommonJS modules), you can check out this in-depth guide. Node.js already exports in-built modules which include fs, path, and http to name a few. But you can create your own modules. Node.js treats each file in a Node project as a module that can export values and functions from the file.

How to share socket Io with other modules in an app?

Since app.js is usually kind of the main initialization module in your app, it will typically both initialize the web server and socket.io and will load other things that are needed by the app. As such a typical way to share io with other modules is by passing them to the other modules in that module's constructor function.


1 Answers

Since app.js is usually kind of the main initialization module in your app, it will typically both initialize the web server and socket.io and will load other things that are needed by the app.

As such a typical way to share io with other modules is by passing them to the other modules in that module's constructor function. That would work like this:

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);             });         });     }; } 

This is what is known as the "push" module of resource sharing. The module that is loading you pushes some shared info to you by passing it in the constructor.

There are also "pull" models where the module itself calls a method in some other module to retrieve the shared info (in this case the io object).

Often, either model can be made to work, but usually one or the other will feel more natural given how modules are being loaded and who has the desired information and how you intend for modules to be reused in other circumstances.

like image 129
jfriend00 Avatar answered Oct 06 '22 01:10

jfriend00