PrivMX DOCS
JavaScript

KVDB Events

Listening for events related to KVDBs and updating your app's state accordingly will allow you to build e.g., instant mapping features.

Types of Events

The lists below outline all the event types available in KVDBs and entries in PrivMX.

KVDB events:

  • kvdbCreated - triggers when a new KVDB is created in your Context
  • kvdbDeleted - triggers when a KVDB is deleted from your Context
  • kvdbUpdated - triggers when a KVDB is updated
  • kvdbStatsChanged - triggers when an entry is created to a KVDB or an entry has been deleted

Entries events:

  • kvdbNewEntry - triggers when an entry was created in a KVDB
  • kvdbEntryDeleted triggers when an entry was deleted from a KVDB
  • kvdbEntryUpdated - triggers when an entry was updated

Handling KVDB and Entries Events

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

Here is an example 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 kvdbApi.subscribeFor([
    // subscribe to `kvdbCreated` events in the given Context
    await kvdbApi.buildSubscriptionQuery(
        KvdbEventType.KVDB_CREATE, 
        KvdbEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),
    // subscribe to `kvdbUpdated` events in the given Context
    await kvdbApi.buildSubscriptionQuery(
        KvdbEventType.KVDB_UPDATE, 
        KvdbEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),
    
    // subscribe to `kvdbEntryDeleted` events in the given KVDB
    await kvdbApi.buildSubscriptionQuery(
        KvdbEventType.ENTRY_DELETE, 
        KvdbEventSelectorType.KVDB_ID, 
        kvdbId
    ),
    // subscribe to `kvdbEntryUpdated` events in the given KVDB
    await kvdbApi.buildSubscriptionQuery(
        KvdbEventType.ENTRY_UPDATE, 
        KvdbEventSelectorType.KVDB_ID, 
        kvdbId
    )
]);

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

KVDB Events | PrivMX Docs