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
| Parameter | Description |
|---|---|
text *Type: StringDefault: n/a | Text that you want to send to the selected channel. |
paramsType: SendTextParamsDefault: SendTextParams() | Publishing options grouped in a single struct. See below for details. |
SendTextParams fields
| Parameter | Description |
|---|---|
metaType: [String: JSONCodable]Default: nil | Publish additional details with the request. |
shouldStoreType: BoolDefault: 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. |
usePostType: BoolDefault: 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. |
ttlType: IntDefault: nil | Defines if / how long (in hours) the message should be stored in Message Persistence.
|
customPushDataType: [String: String]Default: nil | Custom key-value pairs to include in push notification payloads. |
Output
| Parameter | Description |
|---|---|
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.