4

I want to use socket instance from multiple files. I have written my socket connection code in App.js File. Which looks like as follow

var socketCode = require('../api/sockets/socketCode.js'); var io = require('socket.io')(server); io.on('connection', function(socket) { socketCode(io, socket); } 

Now my socketCode.js File looks like follow

module.exports = (io, socket) => { test: function(data) { console.log("io", io, "\n", "socket", socket) return io; } } 

Now I want to call test function from another file let's say from user.js.

My user.js looks like :

var socketCode = require('../api/sockets/socketCode.js')(); console.log(socketCode.test); 

The aim is to keep separate socket code and controllers and other code. Whenever I want to emit some event/message from controller. I just want to call function inside socetCode.js Which should emit the socket event.

How can i call test living inside socketCode.js file from user.js. and it should print/log io object.

I could not find any proper file structure along with socket. I am using express js.

2
  • 1
    You did not express what your actual problem/question is? Commented Nov 29, 2017 at 12:37
  • How to call test function from users.js file? Commented Nov 29, 2017 at 12:48

2 Answers 2

1

As you already figured out how to create the module which exports a named function in socketCode.js:

module.exports = (io, socket) => { test: function(data) { console.log("io", io, "\n", "socket", socket) return io; } } 

You would simply need to import this module somewhere else:

var io = "your io object"; var socket = "your socket object"; var socket = require('../api/sockets/socketCode.js')(io, socket); socket.test(); 

A quick explanation: You are exporting an object with functions as properties. So each function can be considered as member of your imported object.

Sign up to request clarification or add additional context in comments.

12 Comments

i have tried, it does not work. It throws an error test is not a function. I have also tried socket.test that also not worked. And you wrote var socket = require('../api/sockets/socketCode.js')(io, socket); But suppose if i want to require it in any controller files. I would not have io and socket instance there.
@MalikLakhani I am sorry, your module is expecting two parameters (io and socket) which you have to pass into it. I updated my answer
What i want is io should be stored when socketCode.js hit means from app.js file. So when i call socketCode from app.js file. If i log console outside the test(directly inside module.exports) it get printed. Now that io instance should be propagate in test function too. and when i call test function it should use io object.
In a nutshell, Suppose i want to emit event from controller, routes, models. How can i do?
i would not have those parameters inside controllers and similar files.
|
0

There is a library using the Typescript language that will help you to shorten the hard and long way , called socket-controllers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.