M
M
MadmaN132016-05-09 18:00:59
JavaScript
MadmaN13, 2016-05-09 18:00:59

How to subscribe to events in another node.js module?

There is a main module in which the EventEmmiter is created, how to subscribe to this event in the connected module?
The option with passing as a parameter is no longer possible, because this module is not connected directly, but through a chain of connections

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Kitmanov, 2016-05-09
@MadmaN13

You can explicitly put your EventEmitter in the global scope:
Well, access it through global.ee everywhere.
But this is not very good for the architecture. It would be better not to use modules as business entities, but to export some functions, factories, classes from them, then the problem “the module is not connected directly” will cease to make sense.

M
Mikhail Osher, 2016-05-09
@miraage

ioc

E
eQ1, 2018-03-11
@eQ1

// module.js
module.exports = (ee) => {
    ee.on('test', n => console.log(n));
}

//app.js
const {EventEmitter} = require('events');
const ee = new EventEmitter();
require('./module.js')(ee);
...
ee.emit('test');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question