Threaded Messages

The SDK allows you to create relationships between messages. This relationship can be used to implement several features such as message threading, comments and replying to messages.

Message Relationships

Each message has a parentId parameter that signifies that it is a child of another message. With the parentId of a message, you can filter messages to only get the children of that message by passing filterByParentId1:true and parentId in message query.

// Create message with parentId
messageRepo.createTextMessage({
...,
parentId: string
})
// Filter message by parentId
const childMessages = messageRepo.messagesForChannel({
...,
filterByParentId: true,
parentId: string,
})
// Show only messages at top level
const childMessages = messageRepo.messagesForChannel({
...,
filterByParentId: true,
})

For more information on implementing this feature, check out the documentation on:

Use Cases

There are multiple ways you can use parentId. Below are some high-level proof of concepts to show how you can implement these features using the SDK.

Threaded Messages

With filterByParentId you can filter all messages to only show messages at the top level.

// Top level message
{
"message1": {
...message,
"childrenNumber": 2
}
}

When a message has childrenNumber, it means that there are child messages to the parent message. You can query for these child messages by passing the messageId as parentId when querying for messages.

// Child messages
{
"message2": {
...message,
"parentId": "message1"
},
"message3": {
...message,
"parentId": "message1",
"childrenNumber": 1
}
}

We don't limit the depth level of threaded messages, in this case we could again query for all child messages of a child message.

// Child of child message
{
"message4": {
...message,
"parentId": "message3"
},
}

Comments

Implementing a comments system would essentially be the same as implementing threaded message but kept at a single level of depth.

Reply-To and Replies

Since all messages can have a parentId and childrenNumber, implementing a reply-to feature can be done by passing the messageId as parentId to the message that is being replied to.

{
// Original message
"message1": {
...message
},
// Reply message
"message2": {
...message,
"parentId": "message1"
}
}

When we query for these messages, we can check if a message has a parentId. If so we can find that parent message and show the relationship between the two messages in the UI.