PrivMX DOCS
JavaScript

Thread Events

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

Types of Events

The lists below outline all the event types available in Threads and messages in PrivMX.

Thread events:

  • threadCreated - triggers when a new Thread is created in your Context
  • threadDeleted - triggers when a Thread is deleted from your Context
  • threadUpdated - triggers when a Thread is updated
  • threadStatsChanged - triggers when a message is sent to a thread or a message has been deleted

Message events:

  • threadNewMessage - triggers a message was sent to a Thread
  • threadDeletedMessage triggers when a message was deleted from a Thread
  • threadMessageUpdated - triggers when a message was updated

Handling Thread and Message 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 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 threadApi.subscribeFor([
    // subscribe to `threadCreated` events in the given Context
    await threadApi.buildSubscriptionQuery(
        ThreadEventType.THREAD_CREATE, 
        ThreadEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),
    // subscribe to `threadUpdated` events in the given Context
    await threadApi.buildSubscriptionQuery(
        ThreadEventType.THREAD_UPDATE, 
        ThreadEventSelectorType.CONTEXT_ID, 
        CONTEXT_ID
    ),
    
    // subscribe to `threadDeletedMessage` events in the given Thread
    await threadApi.buildSubscriptionQuery(
        ThreadEventType.MESSAGE_DELETE, 
        ThreadEventSelectorType.THREAD_ID, 
        threadId
    ),
    // subscribe to `threadNewMessage` events in the given Thread
    await threadApi.buildSubscriptionQuery(
        ThreadEventType.MESSAGE_CREATE, 
        ThreadEventSelectorType.THREAD_ID, 
        threadId
    )
]);

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

Thread Events | PrivMX Docs