Reading Stored Messages and Sending Read Receipts

This set of APIs deal with reading stored messages in the database, sending read receipts, and syncing messages across devices.

To read stored messages from the database, you need to create a read session and set the criteria to read messages; for example,

  • read all messages
  • read messages from a sender
  • read messages for a particular group
  • read messages matching a search query
  • read messages for a conversation thread
  • etc.

Reading Modes

There are two modes of operation

Reading Messages and Call Logs for a User or a Group

To create a read session in this mode, create a read session from the user or group profile,

MesiboReadSession rs = profile.createReadSession(listener);

Reading the Last Message or Call Log for all Users and Groups

This mode allows you to get a snapshot of all active chats for all users and groups. You can then create another read session to read messages for any of those users or groups.

MesiboReadSession rs = MesiboReadSession.createReadSummarySession(listener);

Reading Order

Once you set a read session, you can start reading messages by calling read API. You can read messages in the first-in-first-out (FIFO) mode or the last-in-first-out (LIFO) mode. In the first-in-first-out mode, the oldest messages are read first. By default, the first-in-first-out mode is disabled.

Read Receipts

You can enable the automatic sending of read receipts every time you read a message. This can be achieved by enabling read-receipt for the read session. On reading or receiving a message, read receipts will be automatically sent if

  • Sending Read Receipt is enabled for the reading session, AND
  • Read receipt requested by the sender, AND
  • A new real-time or database message matches the read session.

Following are the various MesiboReadSession enabler APIs followed by examples. Unless specified, all the read session enabler APIs take only one parameter:

  • enable, Enable or Disable a particular mode

enableUnread

Enable or disable reading only unread messages.

enableFifo

Enable or disable the first-in-first-out mode. Refer to Reading Order above for the details. The enableFifo API is currently not available in Javascript.

enableFiles

Enable reading messages having files. Messages without files will not be read.

enableThreads

Enable or disable Threaded Summary mode. Refer to Reading Modes above for the details.

enableIncomingCalls

Enable or disable reading incoming call logs along with other messages.

enableOutgoingCalls

Enable or disable reading outgoing call logs along with other messages.

enableMissedCalls

Enable or disable reading missed call logs along with other messages.

enableCalls

Enable or disable reading all the call logs along with other messages.

enableReadReceipt

Enable or disable sending read receipts

setQuery

Get messages matching the search query

rs.setQuery("search query");

setThreadId

Get messages matching the conversation thread.

rs.setQuery("search query");

read

Read the requested number of messages. Any subsequent invocations will read more messages till all the messages have been read.

rs.read(count);

It takes the following parameters:

  • count, number of messages to read

sync

Sync the requested number of messages from the server to the local database. Sync is an asynchronous operation, Mesibo_OnSync() listener will be called when sync completes.

It takes the following parameters:

  • count, number of messages to sync
  • Sync Listener The listener context that implements Mesibo_onSync.

setThreadId

Read messages for a particular thread only. Read the Conversation Threads section for more details.

It takes the following parameters:

  • threadid, the thread ID

Read APIs Example

Android Example

To read messages from a user,

// Read receipts are enabled only when App is set to be in the foreground
Mesibo.setAppInForeground(this, 0, true);
MesiboReadSession mReadSession = profile.createReadSession(this);
mReadSession.enableReadReceipt(true);
mReadSession.enableMissedCalls(mShowMissedCalls);
mReadSession.read(100);

To read the last message from each user (summary), in Java

MesiboReadSession mReadSession = MesiboReadSession.createReadSummarySession(this);
mReadSession.enableMissedCalls(mShowMissedCalls);
mReadSession.read(100);

iOS Example

To read messages from a user, In Objective-C,

MesiboReadSession *session = [profile createReadSession:self];
[session enableReadReceipt:YES];
[session read:100];

To read the last message from each user (summary), In Objective-C,

MesiboReadSession *session = [MesiboReadSession createReadSummarySession:self];
[session read:100];

To read messages from a user, In Swift,

let session: MesiboReadSession = profile.createReadSession(self)
session.enableReadReceipt(true)
session.read(100)

To read the last message from each user (summary), In Swift,

let session: MesiboReadSession = MesiboReadSession.createReadSummarySession(self)
session.read(100)

Javascript Example

The way few APIs work in the mesibo Javascript API differs slightly from Android/iOS APIs. This is mainly because of how the local database(IndexDB) operates.

Here is an example in Javascript, to read all messages of an example user, with the address testuser. Replace it with the user address you have. Set the groupid parameter to 0, while reading user messages.

var rs = profile.createReadSession(listener);
rs.enableReadReceipt(true);
rs.read(100);

To read message summary,

var summarySession = MesiboReadSession.createReadSummarySession(listener);
summarySession.read(100);

Synchronization

mesibo provides sync() APIs so that you can download all the messages from the server to the local database and then use normal mesibo read APIs to read messages as required.

It is recommended to use synchronization API on-demand when you run out of messages in the local database. A typical usage would be to use it along with the read() API. The read() API returns the number of messages that were read. If the number of messages is less than you requested, use the sync() API to synchronize the local database with the server. The onSync() function is called with the number of messages synced when sync() is completed.

The sync method available through read Session, takes two parameters:

  • count The number of messages you want to synchronize
  • OnSync Listener that will be called when sync completes (Listener in case of Android/iOS. In case of Javascript, you need to pass an on_sync handler. See Javascript Example).

Note that a call to read() is synchronous while the call to sync() is asynchronous. The result of your sync the request will be available through the listener OnSync. The call to sync checks messages stored on the mesibo server and transfers those messages to your local database. The number of messages that have been synced will be available as a parameter count in the OnSync listener. You can read count number of messages using read().

Synchronization APIs example

Android Example

int readCount = 10;
int result = mReadSession.read(readCount);
if(result < readCount){
        mReadSession.sync(readCount - result, this);
}

@Override
public void Mesibo_onSync(MesiboReadSession rs, int count) {
        // count number of messages have been synced
        // You can now read these messages
        if(count <= 0) return;
        rs.read(count);
}

iOS Example

int readCount = 10;
int result = [mReadSession read:readCount];
if(result < readCount) {
        [mReadSession.sync:readCount - result SyncListener:self];
}

-(void) Mesibo_OnSync:(MesiboReadSession *)rs count:(NSInteger)count {
        // count number of messages have been synced
        // You can now read these messages
        if(count <= 0) return;
        [rs read:count]
}

Javascript Example

In the case of Javascript, you need to pass an on_sync handler as a parameter to sync(), which will be called when sync completes. You can then call read() inside the sync handler.

Mesibo_onMessage(m) {
	if(!m){
		//Run out of messages
		rs.sync(count, function on_sync(rs, count){
				
         });
		return;
	}
}