For example my idea is:
File1.js
io.sockets.on('connection', function (socket) {
socket.on('file1Event', function () {
//logic
});
});
File2.js
io.sockets.on('connection', function (socket) {
socket.on('file2Event', function () {
//logic
});
});
This code it's for a node server, I will have problems it this code?
Another option is to create a rootSocket which handles the initial connection and then passes the socket to other handlers.
Nope, just use the same "io" object.
File1.js
File2.js
app.js
index.html
Be careful not to generate a new connection event for each file. You should use the same on('connection') event, otherwise after 10 files imported, you will get this error from node:
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 connection listeners added. Use emitter.setMaxListeners() to increase limit
.The better way is to do like this in your main file:
and in each separate file: