There is no default way of passing messages between modules. It is quite easy to get something to happen in optionsmod.js when an event occurs in main.js. Simply export the function and call it from main.js. It isn't so straightforward the other way around, though. Two ways I handle this are by passing callback functions and creating event targets. Here's an example with a callback function:
main.js
var optionsmod = require("optionsmod.js");
var eventCallback = function(message) {
console.log('message received: '+message);
};
optionsmod.init(eventCallback);
optionsmod.js
exports.init = function(eventCallback) {
foo.on('bar', function() {
eventCallback('the message');
console.log('message sent');
});
};
The alternative is to export foo
, then call foo.on
from main.js, but that probably defeats the whole purpose of writing a separate module, in which case the docs I linked to will be helpful. Frankly, you could probably use those docs to create your own proprietary messaging system, but I think you're better off thinking in the above terms.