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 ContextthreadDeleted
- triggers when a Thread is deleted from your ContextthreadUpdated
- triggers when a Thread is updatedthreadStatsChanged
- 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 ThreadthreadDeletedMessage
triggers when a message was deleted from a ThreadthreadMessageUpdated
- 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);