Skip to content

eventBus.emit

The emit method allows to emit a given message.

Input parameters

message

message is a mandatory parameter of type BaseMessage which represents a message that should be emitted.

type of the Event Bus messages should always be event

Example

To emit a message using the Event Bus you can use the emit method:

useStorefront(async (storefront) => {
    storefront.eventBus.emit({
        name: 'my-event',
        type: 'event',
        body: {
            description: 'this is my event'
        }
    });
});

Example

In this example we emit a message using the Event Bus and listen to it:

useStorefront(async (storefront) => {
    function myFunction(event) {
        console.log('Got the event exampleField:', event.body.exampleField)
    }

    storefront.eventBus.on('my-event', myFunction);

    storefront.eventBus.emit({
        name: 'my-event',
        type: 'event',
        body: {
            exampleField: 'this is my event'
        }
    });
});

Methods reference