Chat Store

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

First step is to provide StoreFactory instance when initialising the SDK. This should return an instance of 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 'endTransaction' method is called. The underlying data should be synchronised since the transactions can be performed in different threads.

SDK will internally call 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();

Below abstract methods that need to be implemented will be used by the SDK to keep the local chats 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 informations than are required by get methods (this additional data can be safely ignored).

In-Memory ChatStore implementation sample

To implement in memory store we need to 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. Lets create 'Transaction' object that can queue executable Tasks and performs them in an order after execute(data) method is called. You can find the example implementation here - 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. The example implementation can be found here - ChatStoreData.

  3. The last thing is to extend ChatStore class from Comapi Chat SDK and override all it's methods listed in above paragraph. Update/delete/upsert methods should queue the changes stored in Transaction object and when SDK calls 'endTransaction()' method the ChatStoreImplementation will change the data state.

Now on initialisation of 'CoampiChat' in application class you need to set a factory of above ChatStoreImplementation classes link.

Firebase ChatStore implementation

Let's use some database-management system like Realm to store our chat data coming from Comapi Chat SDK.

This time the ChatStore implementation will be simpler since Realm will handle the transactions for us.

The example app can be found on Github.

  1. To work it will require Realm initialisation in Application class and Realm model classes for conversations, messages and message statuses - link.

  2. Then the ChatStoreImplementation will control Realm db update through a single Realm transaction.

  3. Now on initialisation of Comapi Chat in application class you need to set a factory of above ChatStoreImplementation.

  4. The Realm db adapters for the app UI components in this examples will be notified by Realm about any changes made by the SDK to conversations and/or messages.