Skip to main content

Listening for Changes

Listening for events related to 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

Info

The sample code on this page is based on the same assumptions mentioned in Managing Threads.

Before receiving Events, your application has to start an event loop and subscribe to a specific channel. Afterwards you can setup a listener.

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

// handle events
core::EventQueue eventQueue {core::EventQueue::getInstance()};
std::thread t([&](){
while(true) {
core::EventHolder event = eventQueue.waitEvent();
std::cout << "onEvent: " << event.type() << std::endl;
std::cout << event.toJSON() << std::endl;
}
});
t.detach();

// subscribe for threads general events
threadsApi.subscribeForThreadEvents();

// subscribe for particular thread events
threadsApi.subscribeForMessageEvents(threadId);