Handling Events
The @simplito/privmx-webendpoint/extra
package provides utilities for efficiently managing event subscriptions. These utilities simplify event handling, ensuring your application remains reactive and responsive.
EventManager Features
With EventManager
, you can:
- Start an event loop to listen for incoming events.
- Create scoped event managers for specific Tools (e.g., Threads, Stores, Inboxes, connections).
- Dynamically subscribe and unsubscribe from events.
Usage Examples
The example below demonstrates how to use the EventManager
for Thread events.
import { EventManager } from '@simplito/privmx-webendpoint/extra';
// Get the event queue from the endpoint
const eventQueue = await Endpoint.getEventQueue();
// Start the event loop
const eventManager = EventManager.startEventLoop({
waitEvent: eventQueue.waitEvent
});
// Create a scoped event manager for thread events
const threadEventManager = eventManager.getThreadEventManager(YOUR_THREAD_API);
// Subscribe to the 'threadCreated' event
const unsubscribeFromThreadCreated = await threadEventManager.onThreadEvent({
event: 'threadCreated',
callback: (payload) => {
console.log("New thread created:", payload);
}
});
// Subscribe to the 'threadNewMessage' event for a specific thread
const unsubscribeFromMessageCreated = await threadEventManager.onMessageEvent(THREAD_ID, {
event: 'threadNewMessage',
callback: (payload) => {
console.log("New message in thread:", payload);
}
});
// Unsubscribe when needed
await unsubscribeFromThreadCreated();
await unsubscribeFromMessageCreated();
// Stop the event loop when needed
eventManager.stopEventLoop();
For more information regarding Thread-related events please visit Threads docs.