Message threads
Organize conversations into threads for topic-specific discussions.
Benefits:
- Focus discussions on specific topics
- Clarify and resolve issues without cross-talk
- Keep main channel conversations clean
Thread channels have IDs starting with PUBNUB_INTERNAL_THREAD_{channel_id}_{message_id}. Messages with threads have hasThread: true.
ThreadMessage and ThreadChannel extend the base Message and Channel entities with thread-specific methods.
Create thread
createThread(text:params:) creates a thread (channel) for a selected message and sends the first thread message in a single call. It returns both the thread channel and the updated parent message.
Deprecation
Both createThread() (no parameters) and createThread(text:meta:shouldStore:usePost:ttl:quotedMessage:files:usersToMention:customPushData:) are deprecated. Use createThread(text:params:) instead.
Method signature
This method has the following signature:
1message.createThread(
2 text: String,
3 params: SendTextParams = SendTextParams()
4) async throws -> CreateThreadResult<ThreadChannelImpl, MessageImpl>
The CreateThreadResult struct contains:
1public struct CreateThreadResult<TC: ThreadChannel, M: Message> {
2 public let threadChannel: TC
3 public let parentMessage: M
4}
Input
| Parameter | Description |
|---|---|
text *Type: StringDefault: n/a | Text of the first message in the thread. |
paramsType: SendTextParamsDefault: SendTextParams() | Publishing options grouped in a single struct. See sendText() for field descriptions. |
Output
| Parameter | Description |
|---|---|
CreateThreadResultType: CreateThreadResult<ThreadChannelImpl, MessageImpl> | Object containing the thread channel and the updated parent message with hasThread: true. |
→ threadChannelType: ThreadChannelImpl | The newly created thread channel. |
→ parentMessageType: MessageImpl | The parent message updated with thread metadata. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Create a thread for a message on the support channel with an initial reply.
1
Send thread message
Reply to a message in a thread by calling the sendText() method from the previously created ThreadChannel object.
Method signature
Head over to the sendText() method section for details.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Send a message in a thread created for the last message on the support channel.
1
Get thread
Get the thread channel on which the thread message is published.
Method signature
This method has the following signature:
1message.getThread() async throws -> ThreadChannelImpl
Input
This method doesn't take any parameters.
Output
| Type | Description |
|---|---|
ThreadChannelImpl | Object returning the thread channel metadata. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get the thread channel created from the message with the 16200000000000001 timetoken.
1
Check if message starts thread
hasThread indicates if a message starts a thread.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Check if the message with the 16200000000000001 timetoken starts a thread.
1
Get thread message updates
streamUpdatesOn() on ThreadMessage receives updates when thread messages or reactions change.
The callback fires whenever messages are added, edited, deleted, or reactions change. Returns an asynchronous stream.
Stream update behavior
streamUpdatesOn() returns the complete list of monitored thread messages on each change.
Method signature
This method takes the following parameters:
1ThreadMessageImpl.streamUpdatesOn(messages: [ThreadMessageImpl]) -> AsyncStream<[ThreadMessageImpl]>
Input
| Parameter | Description |
|---|---|
messages *Type: [ThreadMessageImpl]Default: n/a | Collection of ThreadMessageImpl objects for which you want to get updates on changed message threads or related message reactions. |
Output
| Parameter | Description |
|---|---|
AsyncStream<[ThreadMessageImpl]> | An asynchronous stream that produces updates when any item in the messages collection is updated. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Get message threads and message reaction-related updates for the first page of messages published in a thread on the support channel.
- AsyncStream
- Closure
1
1
Get thread channel updates
streamUpdatesOn() on ThreadChannel receives updates when thread channels are edited or removed.
The callback fires whenever channel metadata changes. Returns an asynchronous stream.
Stream update behavior
streamUpdatesOn() returns the complete list of monitored thread channels on each change.
Method signature
This method takes the following parameters:
1ThreadChannelImpl.streamUpdatesOn(
2 channels: [ThreadChannelImpl],
3) -> AsyncStream<[ThreadChannelImpl]>
Input
| Parameter | Description |
|---|---|
channels *Type: [ThreadChannelImpl]Default: n/a | Array of ThreadChannelImpl objects for which you want to get updates on changed channel threads. |
Output
| Parameter | Description |
|---|---|
AsyncStream<[ThreadChannelImpl]> | An asynchronous stream that produces updates when any item in the channels collection is updated. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
- AsyncStream
- Closure
1
1
Get historical thread message
getHistory() on ThreadChannel fetches historical thread messages.
Method signature
This method takes the following parameters:
1threadChannel.getHistory(
2 startTimetoken: Timetoken? = nil,
3 endTimetoken: Timetoken? = nil,
4 count: Int = 25
5 ) async throws -> (messages: [ThreadMessageImpl], isMore: Bool)
Input
| Parameter | Description |
|---|---|
startTimetokenType: TimetokenDefault: n/a | Timetoken delimiting the start of a time slice (exclusive) to pull thread messages from. For details, refer to the Fetch History section. |
endTimetokenType: TimetokenDefault: n/a | Timetoken delimiting the end of a time slice (inclusive) to pull thread messages from. For details, refer to the Fetch History section. |
countType: IntDefault: 25 | Number of historical thread messages to return for the channel in a single call. Since each call returns all attached message reactions by default, the maximum number of returned thread messages is 25. For more details, refer to the description of the includeMessageActions parameter in the Swift SDK docs. |
Output
| Parameter | Description |
|---|---|
(messages: [ThreadMessageImpl], isMore: Bool)Type: object | A tuple containing a list of ThreadMessageImpl objects and a boolean flag indicating if there are more messages available. |
By default, each call returns all message reactions and metadata attached to the retrieved thread messages.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Fetch 10 historical thread messages that are older than the timetoken 15343325214676133.
1
Remove thread
removeThread() removes a thread (channel) for a selected message.
Method signature
This method has the following signature:
1message.removeThread() async throws
Input
This method doesn't take any parameters.
Output
| Parameter | Description |
|---|---|
ChannelImpl? | The updated channel object after the removal of the thread, or nil if no channel was returned. |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Remove a thread for the last message on the support channel.
1
Pin thread message to thread channel
pinMessage() on ThreadChannel pins a thread message to the thread channel.
Method signature
This method takes the following parameters:
1threadChannel.pinMessage(
2 message: ThreadMessageImpl
3) async throws -> ThreadChannelImpl
Input
| Parameter | Description |
|---|---|
message *Type: ThreadMessageImplDefault: n/a | ThreadMessageImpl object you want to pin to the selected thread channel. |
Output
| Parameter | Description |
|---|---|
ThreadChannelImpl | Object returning the thread channel metadata updated with these custom fields: pinnedMessageTimetoken to mark the timetoken when the message was pinned pinnedMessageChannelID to mark the channel on which the message was pinned to the thread channel (unpinning was performed either directly on the parent channel or on a thread channel). |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
A thread was created for the last message in the support parent channel. Pin the last message from this thread to the thread channel.
1
Pin thread message to parent channel
pinMessageToParentChannel() (on ThreadChannel) and pinToParentChannel() (on ThreadMessage) pin a thread message to the parent channel.
Method signature
These methods take the following parameters:
-
pinMessageToParentChannel()(on theThreadChannelobject)1threadChannel.pinMessageToParentChannel(
2 message: ThreadMessageImpl
3) async throws -> ChannelImpl -
pinToParentChannel()(on theThreadMessageobject)1threadMessage.pinToParentChannel() async throws -> ChannelImpl
Input
| Parameter | Required in pinMessageToParentChannel() | Required in pinToParentChannel() | Description |
|---|---|---|---|
messageType: ThreadMessageImplDefault: n/a | Yes | No | ThreadMessageImpl object you want to pin to the selected parent channel. |
Output
| Parameter | Description |
|---|---|
ChannelImpl | Object returning the channel metadata updated with these custom fields: pinnedMessageTimetoken to mark the timetoken when the message was pinned pinnedMessageChannelID to mark the channel on which the message was pinned to the parent channel (pinning was performed either directly on the parent channel or on a thread channel). |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
A thread was created for the last message in the support parent channel. Pin the last message from this thread to the parent channel.
-
pinMessageToParentChannel()1 -
pinToParentChannel()1
Unpin thread message from thread channel
unpinMessage() on ThreadChannel unpins the pinned thread message.
Method signature
This method has the following signature:
1threadChannel.unpinMessage() async throws -> ThreadChannelImpl
Input
This method doesn't take any parameters.
Output
| Parameter | Description |
|---|---|
ThreadChannelImpl | Object returning the thread channel metadata updated with these custom fields: pinnedMessageTimetoken to mark the timetoken when the message was unpinned pinnedMessageChannelID to mark the channel on which the message was unpinned from the thread channel (unpinning was performed either directly on the parent channel or on a thread channel). |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Unpin the thread message from the thread (channel) created for the last message on the support channel.
1
Unpin thread message from parent channel
unpinMessageFromParentChannel() (on ThreadChannel) and unpinFromParentChannel() (on ThreadMessage) unpin a thread message from the parent channel.
Method signature
These methods have the following signatures:
-
unpinMessageFromParentChannel()(on theThreadChannelobject)1threadChannel.unpinMessageFromParentChannel() async throws -> ChannelImpl -
unpinFromParentChannel()(on theThreadMessageobject)1threadMessage.unpinFromParentChannel() async throws -> ChannelImpl
Input
These methods don't take any parameters.
Output
| Parameter | Description |
|---|---|
ChannelImpl | Object returning the channel metadata updated with these custom fields: pinnedMessageTimetoken to mark the timetoken when the message was unpinned pinnedMessageChannelID to mark the channel on which the message was unpinned from the parent channel (unpinning was performed either directly on the parent channel or on a thread channel). |
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Unpin the thread message from the support parent channel.
-
unpinMessageFromParentChannel()1 -
unpinFromParentChannel()1