T
T
toxowa2020-01-16 08:12:15
Node.js
toxowa, 2020-01-16 08:12:15

How to create your own events?

Many libraries have the ability to listen to any events occurring somewhere. For example, in ready-made libraries for chatbots, you can do something like ..
client.on("message", callback())
But how to make your own events and subscribe to them in another module? For example, a certain message came or a certain event happened.. Which would trigger the "abracadabra" event in our "nash_client" event. In another module, we could write:
nash_client.on("abracadabra", callback())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-01-16
@toxowa

const EventEmitter = require('events');
const event = new EventEmitter();
event.on('event', (data) => {
  console.log('an event occurred!', data);
});
//usage
event.emit('event', data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question