please tell me how i can add event listener to my service and always when hello action called that event log something . I try to read the documentation but it's not enough.
"use strict";
module.exports = {
name: "greeter",
/**
* Service settings
*/
settings: {
},
/**
* Service dependencies
*/
dependencies: [],
/**
* Actions
*/
actions: {
/**
* Say a 'Hello'
*
* @returns
*/
hello() {
return "Hello Moleculer";
},
},
/**
* Events
*/
events: {
},
/**
* Methods
*/
methods: {
},
/**
* Service created lifecycle event handler
*/
created() {
},
/**
* Service started lifecycle event handler
*/
started() {
},
/**
* Service stopped lifecycle event handler
*/
stopped() {
}
};
@zoheirkhonyagar here is the code.
Please don't use Git Issues for questions. Consider using the chat https://gitter.im/moleculerjs/moleculer
"use strict";
module.exports = {
name: "greeter",
actions: {
hello() {
this.logger.info("Preparing to Emit");
const randomNum = Math.random();
// Emit an actual Event
this.broker.emit("greeter.MyCustomEvent", { num: randomNum });
this.logger.info("Emitted");
return "Hello Moleculer";
}
},
events: {
// Event Listener
"greeter.MyCustomEvent"(payload) {
this.logger.info("Event Caught");
this.logger.info(payload);
}
}
};
Result:

Most helpful comment
@zoheirkhonyagar here is the code.
Please don't use Git Issues for questions. Consider using the chat https://gitter.im/moleculerjs/moleculer
Result:
