Skip to main content
threads

Real-time Communication

Real-time communication requires listening for events related to new messages.

Types of Events

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

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:

// Starting the Event Loop
await Endpoint.startEventLoop();

// Handling Thread events
const threadChannel = await Endpoint.connection().threads.subscribeToThreadEvents();
threadChannel.on('threadCreated', (payload) => console.log(payload));

//Handling message Events
const messageChannel = await Endpoint.connection().thread('THREAD_ID').subscribeToMessageEvents();
messageChannel.on('threadNewMessage', (payload) => console.log(payload));

For JavaScript reference go here.