Categories:
Event management by Aura bridge
Description of how the aura-bridge can emit and listen to events through a centralized service.
Introduction
aura-bridge can emit and listen to events using the AuraBridgeEventService class. This service extends directly from EventEmitter, so any method of this class is available.
How to activate the event service?
The BRIDGE_EVENT_SERVICE_ACTIVE environment variable sets whether or not the event service will be active.
It is possible to continue using the event service even if it is not active. The code will compile and will not produce any errors on execution, but no events will be emitted or received.
Using the event service in a plugin
As for any other service that is necessary in an aura-bridge plugin, it must be added in the plugin configuration:
// plugin package.json file
{
"name": "@telefonica/aura-bridge-example-service",
"version": "1.0.0",
"main": "index.js",
"private": true,
"plugin": {
"consumes": [
"eventService" // <--
],
"provides": [
"exampleService"
]
}
}
Emitting events
To emit an event when an important action must be notified, you can use the emit method:
Services.eventService.emit(WhatsappEvent.AckMessageReceived, eventData);
The events that a plugin emits must be defined in the events field of the service provided by the plugin.
export = registerPlugin([
{
type: PluginType.Processor,
name: 'whatsappIncommingProcessor',
events: [
// Event emitted when a new ack message is received.
WhatsappEvent.AckMessageReceived
]
// ...
}
]);
Listening for events
Once the event service is injected into the plugin, we can listen for events of a specific type using the on method:
Services.eventService.on(WhatsappEvent.AckMessageReceived, async () => {
// process event here
});
⚠️ Check the BRIDGE_EVENT_SERVICE_ACTIVE variable if you are not receiving events