Skip to content

eventBus.on

The on method allows to listen to the event similarly to how addEventListener works for DOM events.

Input parameters

messageName

messageName is a mandatory parameter of the string type which represents the name of the message to listen to.

listener

listener is a mandatory parameter of type TMessageListener which represents a callback for the event emission.

Example

To listen to any event emitted by the Event Bus you can use the on method:

useStorefront(async (storefront) => {
    storefront.eventBus.on('example-event', () => {
        console.log('Listening to the event...')
    });
});

Example

You can have multiple listeners for every event:

useStorefront(async (storefront) => {
    storefront.eventBus.on('example-event', () => {
        console.log('Listening to the event...')
    });

    storefront.eventBus.on('example-event', () => {
        console.log('Listening to the event twice...')
    });

    storefront.eventBus.once('example-event', () => {
        console.log('Listening to the event thrice...')
    });
});

Methods reference