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:
default
- idle state, message is created and stored locally (on client device)syncing
- message is currently syncing to the serversynced
- message is synced and stored on both client and servererror
- 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.
Create a message
Print out the text when the message is created on the server
Start observing a single message immediately
Last updated