PrivMX DOCS
JavaScript

Inbox Events

Events play a crucial role in modern applications, allowing them to instantly respond to changes in stored data. Monitoring and reacting to entries and Inbox-related events, is essential for maintaining up-to-date records, triggering workflows, and ensuring synchronization across various parts of the system.

Types of Events

Before receiving Events, your application must first subscribe to a group of events. In the case of Inbox, types of events are the following:

Inbox events:

  • inboxCreated - triggers when a new Inbox is created in your Context
  • inboxDeleted - triggers when an Inbox is deleted from your Context
  • inboxUpdated - triggers when an Inbox is updated in your Context

Entry events:

  • inboxEntryCreated - triggers when an entry is sent to an Inbox
  • inboxEntryDeleted - triggers when an entry is deleted from an Inbox

Handling Inbox Events

Sample code on this page is based on the initial assumptions

Before receiving Events, your application has to start an event loop and subscribe to specific events in the given scope. Afterwards, you can set up a listener.

Here is an example of how to do it in your own project:

JavaScript
// getting the Event Queue
const eventQueue = await Endpoint.getEventQueue();

// simple function to listen for events in a loop
function listenForEvents(eventsQueue) {
    eventsQueue.waitEvent().then(event => {
        console.log("onEvent", event);
        listenForEvents(eventsQueue);
    });
}
// start listening to events
listenForEvents(eventQueue);

const subscriptions = await inboxApi.subscribeFor([
    // subscribe to `inboxCreated` events in the given Context
    await inboxApi.buildSubscriptionQuery(
        InboxEventType.INBOX_CREATE, 
        InboxEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),
    // subscribe to `inboxUpdated` events in the given Context
    await inboxApi.buildSubscriptionQuery(
        InboxEventType.INBOX_UPDATE, 
        InboxEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),

    // subscribe to `inboxEntryCreated` events in the given Inbox
    await inboxApi.buildSubscriptionQuery(
        InboxEventType.ENTRY_DELETE, 
        InboxEventSelectorType.INBOX_ID, 
        inboxId
    )
]);

We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.

On this page

Inbox Events | PrivMX Docs