On this page

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() creates a thread (channel) for a selected message.

Method signature

This method has the following signature:

1createThread() async throws -> ThreadChannelImpl

Input

This method doesn't take any parameters.

Output

ParameterDescription
ThreadChannelImpl
Object returning the thread channel metadata for the message updated with the hasThread parameter (and a threadRootId action type underneath).

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.

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

* 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 -> ChannelImpl

Input

This method doesn't take any parameters.

Output

ParameterDescription
ChannelImpl
The updated channel object after the removal of the 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.

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: MessageImpl
3) async throws -> ThreadChannelImpl

Input

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

Output

ParameterDescription
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 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
Yes
No
ThreadMessageImpl object you want to pin to the selected parent channel.

Output

ParameterDescription
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// Assuming you have a reference of type "ChatImpl" named "chat"
    2Task {
    3 if let channel = try await chat.getChannel(channelId: "support") {
    4 // Get the last message on the channel, which is the root message for the thread
    5 if let message = try await channel.getHistory(count: 1).messages.first {
    6 // Get the thread channel
    7 let threadChannel = try await message.getThread()
    8 // Get the last message on the thread channel
    9 if let threadMessage = try await threadChannel.getHistory(count: 1).messages.first {
    10 // Pin the message to the parent channel
    11 let updatedChannel = try await threadChannel.pinMessageToParentChannel(message: threadMessage)
    12 debugPrint("Message pinned successfully to the parent channel")
    13 debugPrint("Updated channel object: \(updatedChannel)")
    14 } else {
    15 debugPrint("No messages found in the thread channel")
    show all 23 lines
  • pinToParentChannel()

    1// Assuming you have a reference of type "ChatImpl" named "chat"
    2Task {
    3 if let channel = try await chat.getChannel(channelId: "support") {
    4 // Get the last message on the channel, which is the root message for the thread
    5 if let message = try await channel.getHistory(count: 1).messages.first {
    6 // Get the thread channel
    7 let threadChannel = try await message.getThread()
    8 // Get the last message on the thread channel
    9 if let threadMessage = try await threadChannel.getHistory(count: 1).messages.first {
    10 // Pin the message to the parent channel
    11 let updatedChannel = try await threadMessage.pinToParentChannel()
    12 debugPrint("Message pinned successfully to the parent channel")
    13 debugPrint("Updated channel object: \(updatedChannel)")
    14 } else {
    15 debugPrint("No messages found in the thread channel")
    show all 23 lines

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
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    // Assuming you have a reference of type "ChatImpl" named "chat"
2 Task {
3 if let channel = try await chat.getChannel(channelId: "support") {
4 // Get the last message on the channel, which is the root message for the thread
5 if let message = try await channel.getHistory(count: 1).messages.first {
6 // Get the thread channel
7 let threadChannel = try await message.getThread()
8 // Get the last message on the thread channel
9 if let threadMessage = try await threadChannel.getHistory(count: 1).messages.first {
10 let updatedChannel = try await threadChannel.unpinMessage()
11 debugPrint("Message unpinned successfully from the thread channel")
12 debugPrint("Updated channel object: \(updatedChannel)")
13 } else {
14 debugPrint("No messages found in the thread channel.")
15 }
show all 22 lines

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() -> ChannelImpl
  • unpinFromParentChannel() (on the ThreadMessage object)

    1threadMessage.unpinFromParentChannel() -> ChannelImpl

Input

These methods don't take any parameters.

Output

ParameterDescription
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// Assuming you have a reference of type "ChatImpl" named "chat"
    2Task {
    3 if let channel = try await chat.getChannel(channelId: "support") {
    4 // Get the last message on the channel, which is the root message for the thread
    5 if let message = try await channel.getHistory(count: 1).messages.first {
    6 // Get the thread channel
    7 let threadChannel = try await message.getThread()
    8 // Get the last message on the thread channel
    9 if let threadMessage = try await threadChannel.getHistory(count: 1).messages.first {
    10 let updatedChannel = try await threadChannel.unpinMessageFromParentChannel()
    11 debugPrint("Message unpinned successfully from the parent channel")
    12 debugPrint("Updated channel object: \(updatedChannel)")
    13 } else {
    14 debugPrint("No messages found in the thread channel")
    15 }
    show all 22 lines
  • unpinFromParentChannel()

    1Task {
    2 if let channel = try await chat.getChannel(channelId: "support") {
    3 // Get the last message on the channel, which is the root message for the thread
    4 if let message = try await channel.getHistory(count: 1).messages.first {
    5 // Get the thread channel
    6 let threadChannel = try await message.getThread()
    7 // Get the last message on the thread channel
    8 if let threadMessage = try await threadChannel.getHistory(count: 1).messages.first {
    9 let updatedChannel = try await threadMessage.unpinFromParentChannel()
    10 debugPrint("Message unpinned successfully from the parent channel")
    11 debugPrint("Updated channel object: \(updatedChannel)")
    12 } else {
    13 debugPrint("No messages found in the thread channel")
    14 }
    15 } else {
    show all 21 lines
Last updated on