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. default - idle state, message is created and stored locally (on client device)

  2. syncing - message is currently syncing to the server

  3. synced - message is synced and stored on both client and server

  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. Create a message

  2. Print out the text when the message is created on the server

  3. Start observing a single message immediately

let messageId = messageRepository.createTextMessage(...) { message, error in
     // Handle message creation result.
     if let message = message {
         print("The message is created on the server.")
     } else {
         print("Error: \(error).")
     }
}

// ⬆ .createTextMessage(...) above returns messageId.
//
// If the app already observes a message live collection via `.getMessages(...)`
// This message will appear on those live collections immediately.
//
// For the use-cases that require observing a single message after creation,
// Developers can use this messageId and pass it to `.getMessage(...)`.
//
token = messageRepository.getMessage(messageId)?.observe { liveMessage, error in
    // Observe message data changes.
    guard let message = liveMessage.object else {
        return
    }
    switch message.syncState {
    case .synced:
        print("The message was created on the server.")
    default:
        print("syncState = \(message.syncState)")    
    }
    updateUI(with: message)
}

Last updated