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​

* required
ParameterDescription
text *
Type: String
Default:
n/a
Text of the first message in the thread.
params
Type: SendTextParams
Default:
SendTextParams()
Publishing options grouped in a single struct. See sendText() for field descriptions.

Output​

ParameterDescription
CreateThreadResult
Type: CreateThreadResult<ThreadChannelImpl, MessageImpl>
Object containing the thread channel and the updated parent message with hasThread: true.
 → threadChannel
Type: ThreadChannelImpl
The newly created thread channel.
 → parentMessage
Type: 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​

TypeDescription
ThreadChannelImplObject 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​

* required
ParameterDescription
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​

ParameterDescription
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.

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​

* required
ParameterDescription
channels *
Type: [ThreadChannelImpl]
Default:
n/a
Array of ThreadChannelImpl objects for which you want to get updates on changed channel threads.

Output​

ParameterDescription
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.

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​

* required
ParameterDescription
startTimetoken
Type: Timetoken
Default:
n/a
Timetoken delimiting the start of a time slice (exclusive) to pull thread messages from. For details, refer to the Fetch History section.
endTimetoken
Type: Timetoken
Default:
n/a
Timetoken delimiting the end of a time slice (inclusive) to pull thread messages from. For details, refer to the Fetch History section.
count
Type: Int
Default:
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​

ParameterDescription
(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​

ParameterDescription
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​

* required
ParameterDescription
message *
Type: ThreadMessageImpl
Default:
n/a
ThreadMessageImpl object you want to pin to the selected thread channel.

Output​

ParameterDescription
ThreadChannelImplObject 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 the ThreadChannel object)

    1threadChannel.pinMessageToParentChannel(
    2 message: ThreadMessageImpl
    3) async throws -> ChannelImpl
  • pinToParentChannel() (on the ThreadMessage object)

    1threadMessage.pinToParentChannel() async throws -> ChannelImpl

Input​

ParameterRequired in pinMessageToParentChannel()Required in pinToParentChannel()Description
message
Type: ThreadMessageImpl
Default:
n/a
YesNoThreadMessageImpl object you want to pin to the selected parent channel.

Output​

ParameterDescription
ChannelImplObject 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​

ParameterDescription
ThreadChannelImplObject 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 the ThreadChannel object)

    1threadChannel.unpinMessageFromParentChannel() async throws -> ChannelImpl
  • unpinFromParentChannel() (on the ThreadMessage object)

    1threadMessage.unpinFromParentChannel() async throws -> ChannelImpl

Input​

These methods don't take any parameters.

Output​

ParameterDescription
ChannelImplObject 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
    

Was this page useful?

Last updated on