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
SendTextParams fields
meta#- Publish additional details with the request.
shouldStore#- If
true, the messages are stored in Message Persistence.
IfshouldStoreis not specified, the Message Persistence configuration specified on the Admin Portal keyset is used. usePost#- 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#- Defines if / how long (in hours) the message should be stored in Message Persistence.
- If
shouldStore=true, andttl=0, the message is stored with no expiry time. - If
shouldStore=trueandttl=X, the message is stored with an expiry time ofXhours unless you have message retention set toUnlimitedon your keyset configuration in the Admin Portal. - If
shouldStore=false, thettlparameter is ignored. - If
ttlis not specified, then the expiration of the message defaults back to the expiry value for the keyset.
- If
customPushData#- 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.