Skip to main content

com.sabre.redapp.example3.web.eventbus.handler Sample

What is event bus

SR360 offers access to the global event bus. You can use it to communicate between system components that do not know anything about each other. One component (Red App in our case) sends (triggers) the event, with any given name, on the event bus and forgets about it. Any other component can listen to an event with a given name and execute business logic when it occurs. This keeps components loosely coupled. Keep in mind that such communication is asynchronous and one-directional.

Sending custom event

EventBusService can be used to generate an event, with the triggerOnEventBus method. It requires one mandatory parameter - event name. Be aware that event bus is global and shared between core SR360 events and all Red Apps, so to avoid confusion all events triggered by a Red App have to be prefixed with the Red App name.

The second, optional parameter is an object with the data we want to send with the event.

    fireEvent(): void {

        const eb = getService(EventBusService);

        eb.triggerOnEventBus("com-sabre-redapp-example3-web-eventbus-trigger-sample-event",
            {
                data: "Hi there!"
            }
        );
    }

Handling a custom event

eb.on("com-sabre-redapp-example3-web-eventbus-trigger-sample-event", (data) => {
            eventCount = eventCount + 1;
            areaService.showBanner('Info', 'Message from event: ' + JSON.stringify(data) + '. Event count: ' + eventCount);
        });

Case Study - communication between two Red Apps

Events can be used to communicate between two Red Apps.

Triggerer Sample

com.sabre.redapp.example3.web.eventhandler.trigger

import {Module} from 'sabre-ngv-core/modules/Module';
import {getService} from './Context';
import {RedAppSidePanelConfig} from 'sabre-ngv-xp/configs/RedAppSidePanelConfig';
import {RedAppSidePanelButton} from 'sabre-ngv-redAppSidePanel/models/RedAppSidePanelButton';
import {ExtensionPointService} from 'sabre-ngv-xp/services/ExtensionPointService';
import {EventBusService} from 'sabre-ngv-app/app/services/impl/EventBusService';

export class Main extends Module {


    init(): void {

        super.init();

        const xp = getService(ExtensionPointService);

        const sidepanelConfig = new RedAppSidePanelConfig([
            new RedAppSidePanelButton('Trigger Event', 'btn btn-secondary side-panel-button redapp-web-eventbus', () => this.fireEvent())
        ]);

        xp.addConfig('redAppSidePanel', sidepanelConfig);


    }

    fireEvent(): void {

        const eb = getService(EventBusService);

        eb.triggerOnEventBus("com-sabre-redapp-example3-web-eventbus-trigger-sample-event",
            {
                data: "Hi there!"
            }
        );

        console.log("Event fired");
    }

}

Handler Sample

com.sabre.redapp.example3.web.eventhandler.handler

import {Module} from 'sabre-ngv-core/modules/Module';
import {getService} from './Context';
import {EventBusService} from 'sabre-ngv-app/app/services/impl/EventBusService';
import {IAreaService} from 'sabre-ngv-app/app/services/impl/IAreaService';
import {SignInEventPublic} from 'sabre-ngv-signin-event-trigger/SignInEventPublic';

export class Main extends Module {


    init(): void {

        super.init();

        const eb = getService(EventBusService);

        const areaService: IAreaService = getService(IAreaService);

        let eventCount = 0;

        eb.listenToEventBus("com-sabre-redapp-example3-web-eventbus-trigger-sample-event", (data) => {
            eventCount = eventCount + 1;
            areaService.showBanner('Info', 'Message from event: ' + JSON.stringify(data) + '. Event count: ' + eventCount);
        });

        eb.listenToEventBus(SignInEventPublic, () => {
            areaService.showBanner('Info', 'User Signed In');
            console.log("New user signed in");
        })

        console.log("Handler registered");


    }

}