Chat store

Messaging events and service responses are redirected to a store interface implementation inside your app that you register on SDK initialisation.

The first step is to provide a StoreFactory instance when initialising the SDK. This should return an instance of the ChatStore class with implementation of its abstract methods.

// Sets persistence store factory. 
//ChatStore instance will queue db changes and perform them when {@link ChatStore#endTransaction} has been called internally.
chatConfig.store(new StoreFactory<ChatStore>() {
	@Override
	protected void build(StoreCallback<ChatStore> callback) {
		callback.created(new ChatStoreImplementation());
	}
});

// Initialise SDK in onCreate method in Application class.
ComapiChat.initialise(app, chatConfig, callback);
// Implementation of chat store class that provides a way to store data relevant both to SDK and the app.
public class ChatStoreImplementation extends ChatStore {
  // implementation off all abstract methods used to modify you db by Comapi Chat SDK.
}

This implementation should queue changes in a single pending transaction and perform the db inserts/update when the endTransaction method is called. The underlying data should be synchronised, since the transactions can be performed in different threads.

The SDK internally calls the below methods to start queuing changes and to tell the store to apply the changes.

// begin transaction, from this point calling other methods of this class will queue store updates and inserts to be executed when {@link ChatStore#endTransaction()} on same instance is called
beginTransaction();

// end transaction, execute queued store updates and inserts
endTransaction();

The below abstract methods that need to be implemented are used by the SDK to keep the local chat data inside your database consistent with the server.

// get conversation
getConversation(String conversationId);

// get all conversations
getAllConversations();

// insert or update conversation in persistence store
upsert(ChatConversation conversation);

// update conversation
update(ChatConversationBase conversation);

// delete conversation
deleteConversation(String conversationId);

// insert or update message
upsert(ChatMessage message);

// update stored {@link ChatMessage} with a new {@link ChatMessageStatus}. The chat message is unique for a combination of messageId, profileId and {@link LocalMessageStatus} value
update(ChatMessageStatus status);

// delete all messages from persistence store that are related to given conversation
deleteAllMessages(String conversationId);

// delete message
deleteMessage(String conversationId, String messageId);

// delete all content of persistence store
clearDatabase();

The upsert and update methods can provide more information than is required by GET methods. This additional data can be safely ignored.


In-memory ChatStore implementation sample

To implement in memory store, you must create some mechanism of updating the data in a single transaction. The operations triggered by the SDK to update db between the calls beginTransaction() and endTransaction() should be queued and executed after the endTransaction().

The example app can be found on GitHub.

  1. Create a Transaction object that can queue executable tasks and performs them in an order after the execute(data) method is called.
    Example implementation: Transaction
  2. The data itself modified with the in Transaction class can be stored in couple maps with the keys being object IDs for faster queries.
    Example implementation: ChatStoreData
  3. The last thing is to extend the ChatStore class from the Chat SDK and override all its methods listed above. Update/delete/upsert methods should queue the changes stored in the Transaction object and when the SDK calls the endTransaction() method the ChatStoreImplementation changes the data state.

Now on initialisation of ComapiChat in application class, you need to set a factory of above ChatStoreImplementation classes.

Learn more in SDK Android samples.


Firebase ChatStore implementation

You can use a database management system like Realm to store chat data coming from our Chat SDK.

This time the ChatStore implementation is more straightforward since Realm will handle the transactions for you.

The example app can be found on Github.

  1. To work, it requires Realm initialisation in the Application class and Realm model classes for conversations, messages, and message statuses.
    Learn more in SDK Android samples.
  2. The ChatStoreImplementation controls the Realm db update through a single Realm transaction.
  3. On initialisation of Comapi Chat in the application class you must set a factory of above ChatStoreImplementation.
  4. The Realm db adapters for the app UI components in the examples are notified by Realm about any changes made by the SDK to conversations and/or messages.