Event API
Contents
This API provides functions to create and listen to instance local or cluster distributed events.
Usage
Add the following to your build.gradle file:
dependencies {
  include "com.enonic.xp:lib-event:${xpVersion}"
}
    Add the import statement to your controller:
import eventLib from '/lib/xp/event';
    You are now ready to use the API.
| 
         Several standard XP APIs already produce and publish events. For more details, check out the node API, repository API, and task API  | 
      
Functions
listener
Creates an event listener
Parameters
An object with the following keys and their values:
| Name | Kind | Details | 
|---|---|---|
| 
         type  | 
       
         string  | 
       
         Works like a Java Pattern, with two key differences:   | 
      
| 
         callback  | 
       
         function  | 
       
         Callback event listener  | 
      
| 
         localOnly  | 
       
         boolean  | 
       
         Local events only (default to false)  | 
      
Returns
null
Example
eventLib.listener({
    type: 'node.*',
    localOnly: false,
    callback: (event) => {
        log.info('event: %s', JSON.stringify(event));
    }
});
     send
Sends a custom event. All custom events are prefixed with custom..
Parameters
An object with the following keys and their values:
| Name | Kind | Details | 
|---|---|---|
| 
         type  | 
       
         string  | 
       
         Event type  | 
      
| 
         distributed  | 
       
         boolean  | 
       
         
  | 
      
| 
         data  | 
       
         object  | 
       
         Additional data for event.  | 
      
Returns
null
Example
eventLib.send({
    type: 'myEvent',
    distributed: false,
    data: {
        a: 1,
        b: 2
    }
});