Send and receive messages

Requires Message Persistence

To store data about messages, you must enable Message Persistence for your app's keyset in the Admin Portal and set the desired message retention period.

Send

Send a message to a previously defined channel using the sendText() method.

sendText() prepares the final message content for publishing, including text, any attached files, metadata, mentioned users, or referenced channels.

Method signature

This method takes the following parameters:

channel.sendText(
text: String,
meta: [String: JSONCodable]? = nil,
shouldStore: Bool = true,
usePost: Bool = false,
ttl: Int? = nil,
quotedMessage: ChatType.ChatMessageType? = nil,
files: [InputFile]? = nil,
usersToMention: [String]?
) async throws -> Timetoken

Input

* required
ParameterDescription
text *
Type: String
Default:
n/a
Text that you want to send to the selected channel.
meta
Type: [String: JSONCodable]
Default:
n/a
Publish additional details with the request.
shouldStore
Type: Bool
Default:
true
If true, the messages are stored in Message Persistence.
If shouldStore is not specified, the Message Persistence configuration specified on the Admin Portal keyset is used.
usePost
Type: Bool
Default:
false
When true, the SDK uses HTTP POST to publish the messages. The message is sent in the BODY of the request instead of the query string when HTTP GET is used. The messages are also compressed to reduce their size.
ttl
Type: Int
Default:
n/a
Defines if / how long (in hours) the message should be stored in Message Persistence.
  1. If shouldStore = true, and ttl = 0, the message is stored with no expiry time.
  2. If shouldStore = true and ttl = X, the message is stored with an expiry time of X hours unless you have message retention set to Unlimited on your keyset configuration in the Admin Portal.
  3. If shouldStore = false, the ttl parameter is ignored.
  4. If ttl is not specified, then the expiration of the message defaults back to the expiry value for the keyset.
quotedMessage
Type: ChatType.ChatMessageType
Default:
n/a
Object added to a message when you quote another message. This object stores the following info about the quoted message: timetoken for the time when the quoted message was published, text with the original message content, and userId as the identifier of the user who published the quoted message.

Refer to Quotes for more details and example.
files
Type: [InputFile]
Default:
n/a
One or multiple files attached to the text message. Each InputFile contains a name, type, and source: public struct InputFile(public var name: String, public var type: String, public var source: PubNub.FileUploadContent).

Refer to Files for more details and example.
usersToMention
Type: [String]
Default:
n/a
A collection of user IDs to automatically notify with a mention after this message is sent. Refer to Mentions for more details and example.

Output

ParameterDescription
Timetoken
Returned of the published message.

Basic usage

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 the Hi Everyone! message to the support channel. Mark its high priority and state that it should be stored in Message Persistence for 15 hours.

// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
let timetoken = try await channel.sendText(
text: "Hi Everyone",
meta: ["messageImportance": "high"], // Custom metadata
shouldStore: true, // Store in Message Persistence
ttl: 15 // 15 hours
)
debugPrint("Message sent successfully at \(timetoken)")
} else {
debugPrint("Channel not found")
}
}

Receive

To receive messages on a given channel, you must connect to the channel and start listening to message events.

Last updated on