So, I'm making an app and I have gotten socket IO working and the app just runs on Express.
Anyway, within the main server file is where I am doing my calls. Some examples:
//Object 1
io.of('/type1').on
( 'connection', socket =>
{ socket.on
( 'call1', () =>
{ doSomething1();
}
);
}
);
// Object 2
io.of('/type2').on
( 'connection', socket =>
{ socket.on
( 'call2', () =>
{ doSomething2();
}
);
}
);
Obviously, there are way more socket.on calls for the various objects. What I wanted to do was actually separate these objects' logic from the main loop into some separate files to better clean up the main server.js file I have. I've got some 300 lines of various socket calls.
Something like socketCalls1.js for Object 1, etc.
The only thing is that the io variable doesn't seem sharable across multiple files... Is there a good, clean way of doing this?
This is not true. You can pass the
io
variable into functions and out of modules just like any other variable. Also like any other variable it is automatically privately scoped to a single file.There are several ways to share objects across files and if you've gotten this far I'm sure you've used most of them. Only maybe you may not have realized what you have been doing.
Method 1: pass
io
into functions.One way to split your logic is to split it across several modules. Since you can pass objects into functions your modules should export functions. For example:
file1: type1.js
file2: type2.js
main.js
This works because
io
is just a regular variable. And just like any variable you can pass it as a function argument. Nothing here is fancy, this is all basic features of programming languages.Method2: share
io
as a module.Another technique is specific to how modules work in node.js. Modules are singletons in node. What that means is that a module represents exactly one object. If 3 different files
require()
s the module they will all get the same object.We can use this to share the
io
object between modules:file1: server-init.js
file2: type1.js
file3: type2.js
main.js