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]?,
completion: ((Swift.Result<Timetoken, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
text | String | Yes | n/a | Text that you want to send to the selected channel. |
meta | [String: JSONCodable] | No | n/a | Publish additional details with the request. |
shouldStore | Bool | No | 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 | Bool | No | 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 | Int | No | n/a | Defines if / how long (in hours) the message should be stored in Message Persistence.
|
quotedMessage | ChatType.ChatMessageType | No | 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 | [InputFile] | No | 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 | [String] | No | 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
Type | Description |
---|---|
Timetoken | Returned of the published message. |
Basic usage
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 "chat" instance available
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Prepare the meta dictionary for high priority
let meta: [String: JSONCodable] = ["messageImportance": "high"]
/// Send the "Hi Everyone!" message with high priority and store it for 15 hours
channel.sendText(
text: "Hi Everyone!",
meta: meta, /// Mark high priority
shouldStore: true, /// Store in Message Persistence
show all 31 linesReceive
To receive messages on a given channel, you must connect
to the channel and start listening to message events.
Send (deprecated)
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,
mentionedUsers: MessageMentionedUsers? = nil,
referencedChannels: MessageReferencedChannels? = nil,
textLinks: [TextLink]? = nil,
quotedMessage: ChatType.ChatMessageType? = nil,
files: [InputFile]? = nil,
completion: ((Swift.Result<Timetoken, Error>) -> Void)? = nil
)
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
text | String | Yes | n/a | Text that you want to send to the selected channel. |
meta | [String: JSONCodable] | No | n/a | Publish additional details with the request. |
shouldStore | Bool | No | 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 | Bool | No | 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 | Int | No | n/a | Defines if / how long (in hours) the message should be stored in Message Persistence.
|
mentionedUsers | MessageMentionedUsers | No | n/a | Object mapping a mentioned user (with name and ID) with the number of mention (like @Mar ) in the message (relative to other user mentions). For example, { 0: { id: 123, name: "Mark" }, 2: { id: 345, name: "Rob" } } means that Mark will be shown on the first mention (@ ) in the message and Rob on the third. Refer to User mentions for more details and example. |
referencedChannels | MessageReferencedChannels | No | n/a | Object mapping the referenced channel (with name and ID) with the place (Int ) where this reference (like #Sup ) was mentioned in the message (relative to other channel references). For example, { 0: { id: 123, name: "Support" }, 2: { id: 345, name: "Off-topic" } } means that Support will be shown on the first reference in the message and Off-topic on the third. Refer to Reference channels for more details and example. |
textLinks | [TextLink] | No | n/a | Returned list of text links that are shown as text in the message Each TextLink contains these fields: public struct TextLink(public var startIndex: Int, public var endIndex: Int, public var link: String) where startIndex indicates the position in the whole message where the link should start and endIndex where it ends. Note that indexing starts with 0 .Refer to Links for more details and example. |
quotedMessage | ChatType.ChatMessageType | No | 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 | [InputFile] | No | 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. |
Output
Type | Description |
---|---|
Timetoken | Returned of the published message. |
Basic usage
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 "chat" instance available
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Prepare the meta dictionary for high priority
let meta: [String: JSONCodable] = ["messageImportance": "high"]
/// Send the "Hi Everyone!" message with high priority and store it for 15 hours
channel.sendText(
text: "Hi Everyone!",
meta: meta, /// Mark high priority
shouldStore: true, /// Store in Message Persistence
show all 31 lines