Send and receive messages

Requires Message Persistence

Enable Message Persistence on your keyset in the Admin Portal to store messages.

Send​

Use sendText() to send a message to a channel. Pass additional publishing options in a SendTextParams struct. To attach files, add user mentions, or quote messages, use a MessageDraft instead.

Method signature​

This method takes the following parameters:

1channel.sendText(
2 text: String,
3 params: SendTextParams = SendTextParams()
4) async throws -> Timetoken

The SendTextParams struct groups common publishing options:

1public struct SendTextParams {
2 public var meta: [String: JSONCodable]?
3 public var shouldStore: Bool
4 public var usePost: Bool
5 public var ttl: Int?
6 public var customPushData: [String: String]?
7}

Input​

required
text required#
Type: String
Default: no default
Text that you want to send to the selected channel.
params#
Type: SendTextParams
Default: SendTextParams()
Publishing options grouped in a single struct. See below for details.

SendTextParams fields​

meta#
Type: [String: JSONCodable]
Default: nil
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: nil
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.
customPushData#
Type: [String: String]
Default: nil
Custom key-value pairs to include in push notification payloads.

Output​

Timetoken#
Returned timetoken of the published message.

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

1

Receive​

To receive messages on a given channel, use onMessageReceived() (closure-based) or channel.stream.messages() (AsyncStream-based) to start listening to message events.

Was this page useful?

Last updated on