Event Library

Contents

This library contains 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}"
}

In your JavaScript controller, add a require statement:

const eventLib = require('/lib/xp/event');

You are now ready to use the library functionality.

Several standard XP APIs already produce and publish events. For more details, check out the node API, repository API, and task API

Functions

listener

Adds an event listener to the system

Parameters

An object with the following keys and their values:

Name Kind Details

type

string

Event type pattern

callback

function

Callback event listener

localOnly

boolean

Local events only (default to false)

Returns

null

Example

Adding a listener that logs the event
eventLib.listener({
    type: 'node.*',
    localOnly: false,
    callback: function (event) {
        log.info(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

True if it should be distributed in cluster

data

object

Additional data for event.

Returns

null

Example

Send a custom event into the system
eventLib.send({
    type: 'myEvent',
    distributed: false,
    data: {
        a: 1,
        b: 2
    }
});

Contents

Contents