Creating Messages
All message sending methods are designed to be robust enough to work under any network conditions. When you send any message, that message will automatically be put into a queue in case there are unforeseen unstable network conditions. Once the SDK reconnects to the server, it will automatically resend all the queued messages.
Additionally, sent messages are always returned in message queries, even before they have been delivered to the server. This provides the user with a fluid messaging flow: when a user sends a message, that sent message will appear in the message stream right away (instead of waiting until it has been confirmed by the server). To check or display the current status of message delivery for your application, use the syncState property in the message model.
There are four statuses of syncState:
  1. 1.
    default - idle state, message is created and stored locally (on client device)
  2. 2.
    syncing - message is currently syncing to the server
  3. 3.
    synced - message is synced and stored on both client and server
  4. 4.
    error - failed to sync message
Every message sending method returns you a live object. This live object should be observed until message has been synced with the server.
Amity supports the sending and receiving of five types of messages:

Message Creation Pattern

The SDK first creates a message optimistically on local without waiting for the server response. This message can immediately appear in collections that it belongs to, for example in a message list of its channel via Message Query API.
Simultaneously, the SDK sends a request to create the same message on the server. The SDK will then notify the server response via completion.
Developers can use message.syncState to check whether the message has been created on the server or not.

Sample Code

This sample code shows the general pattern to work with message creation API.
  1. 1.
    Create a message
  2. 2.
    Print out the text when the message is created on the server
  3. 3.
    Start observing a single message immediately
1
let messageId = messageRepository.createTextMessage(...) { message, error in
2
// Handle message creation result.
3
if let message = message {
4
print("The message is created on the server.")
5
} else {
6
print("Error: \(error).")
7
}
8
}
9
10
// ⬆ .createTextMessage(...) above returns messageId.
11
//
12
// If the app already observes a message live collection via `.getMessages(...)`
13
// This message will appear on those live collections immediately.
14
//
15
// For the use-cases that require observing a single message after creation,
16
// Developers can use this messageId and pass it to `.getMessage(...)`.
17
//
18
token = messageRepository.getMessage(messageId)?.observe { liveMessage, error in
19
// Observe message data changes.
20
guard let message = liveMessage.object else {
21
return
22
}
23
switch message.syncState {
24
case .synced:
25
print("The message was created on the server.")
26
default:
27
print("syncState = \(message.syncState)")
28
}
29
updateUI(with: message)
30
}
Copied!
Export as PDF
Copy link