0

I'm working on a node module and it runs two servers, a http one and a socket.io one.
My http server is located in lib/http and my socket.io one is located in lib/ws.
I have a file called bin/dsmnet.

In that file I invoke my http and socket.io server but I have a variable called users and I need both servers to be able to modify the variable and to share it.
Here is my current code:

var dhttp = require('../lib/http');
var ws = require('../lib/ws');
var start = function (users, key, loglevel){   
    //Starts WebSocket and HTTP Server
    dhttp.listen(users, 3265);
    ws.listen(RSAkey, users, 3266);
}

Now I need to share the users variable between the two functions which are both located in two separate files.
Should I merge the http and socket.io server into one function and file so they can both use the same variable or is there another solution.

EDIT: To clarify I want to be able to append an array to my variable in my ws function in ws.js and read the array in my dhttp function in http.js and vice versa.

C1D
  • 3,034
  • 2
  • 21
  • 20
  • Use dependency injection and pass `Users` to both modules - see http://stackoverflow.com/a/18474689/1348195 – Benjamin Gruenbaum Sep 05 '13 at 10:21
  • I don't understand how the dependency injection works? – C1D Sep 05 '13 at 10:47
  • Both are different modules, you pass Users explicitly to the function you're using to create dhttp or ws. – Benjamin Gruenbaum Sep 05 '13 at 10:56
  • Okay. I passed users to the start function but I can't access it in my other functions. I passed it two both functions but if I change the users variable in dhttp, ws doesn't have the same variable value. – C1D Sep 05 '13 at 11:21
  • I found out how to fix it. I did what you said but instead used an Object. Thanks :) – C1D Sep 05 '13 at 12:00
  • You're welcome. I suggest you read the answer on the other question - that explains the merits of this approach :) – Benjamin Gruenbaum Sep 05 '13 at 14:38

0 Answers0