Get Started: Reading Messages

Estimated reading time: 8 minutes

In this part, we will learn about storing and reading messages in mesibo.

Prerequisites

You MUST go through the following prerequisites before you read further.

  • Read the First App Guide.
  • Read one of the Android, iOS, JS section of this tutorial.

How to store messages

In mesibo a message can be anything- a text message, a location, a file(like image, video, etc), or any arbitrary binary data. There are three types of storage mesibo uses to store your data:

  • Local Device Storage: The messages sent and received by a user can be stored in a local database at the user end for faster access. You can enable a local database to store all incoming & outgoing messages and other information by using setDatabse while initializing mesibo.

  • Server-Side Storage: When User A sends a message to User B and if User B is not online, mesibo stores messages on the server till User B comes online. Once the message is delivered to User B, it is deleted from the server unless the retention option is enabled. If you enable the retention option, all the communication between your users is stored on the server. If you are hosting mesibo on your servers using mesibo on-premise, all the messages will be saved on your own database.

  • File Storage: All the files you transfer using mesibo are stored in a file server. The mesibo does not mandate where you store your files. You can store your files on your private file server or AWS, Azure, or any such cloud hosts. You need to write a file handlers so that mesibo can upload to or download files from your server.

How to read messages

Mesib provides a set of APIs deal with stored messages in the database and sending read receipts.

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 etc.

The read session you create filters the messages in the database based on the criteria provided and gives the resulting list of messages to you.

Reading Modes

There are two modes of operation:

  • Read Messages and call logs. This mode is enabled by default
  • Read Summary (active conversations), read the latest message from each user and group. This allows you to get a snapshot of all the communicating users and groups. You can then create another read session to read messages for any of those users or groups.

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 model, 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 a read-receipt for the read session. On reading or receiving a message, read receipts will be automatically sent if

  • The app is set to be in the foreground
  • 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.

A call to read will return the number of messages read. You can call read on demand. For example, in the case of a messaging app, you only need to show the latest messages on the screen initially. So, first, you can call read(10) to load the last 10 messages. As the user scrolls up the chat history, you can call read again. All subsequent calls to read will read older messages.

Android Example

To read messages for a user or a group, in Java

// Read receipts are enabled only when App is set to be in foreground
Mesibo.setAppInForeground(this, 0, true);
Mesibo.ReadDbSession mReadSession = new Mesibo.ReadDbSession(from, 0, null, this);
mReadSession.enableReadReceipt(true);
mReadSession.enableMissedCalls(mShowMissedCalls);
mReadSession.read(100);

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

Mesibo.ReadDbSession mReadSession = new Mesibo.ReadDbSession(null, 0, null, this);
mDbSession.enableSummary(true);
mReadSession.enableReadReceipt(false);
mReadSession.enableMissedCalls(mShowMissedCalls);
mReadSession.read(100);

To read messages for a user or a group, in Kotlin

// Read receipts are enabled only when App is set to be in foreground
Mesibo.setAppInForeground(this, 0, true)
mReadSession = Mesibo.ReadDbSession(mRemoteUser!!.address, this)
mReadSession?.enableReadReceipt(true)
mReadSession?.read(100)

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

mReadSession = Mesibo.ReadDbSession(null, this)
mReadSession?.enableReadReceipt(true)
mReadSession?.enableMissedCalls(true)
mReadSession?.read(100)

iOS Example

To read messages for a user or a group, In Objective-C,

MesiboReadSession *mReadSession = [MesiboReadSession new];
[mReadSession initSession:mRemoteUser groupid:0 query:nil delegate:self];
[mReadSession enableReadReceipt:YES];
[mReadSession read:100];

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

MesiboReadSession *mReadSession = [MesiboReadSession new];
[mReadSession initSession:nil groupid:0 query:nil delegate:self];
[mReadSession enableSummary:YES];
[mReadSession read:100];

To read messages for a user or a group, In Swift,

mReadSession = MesiboReadSession()
mReadSession.initSession(mRemoteUser, groupid: 0, query: nil, delegate: self)
mReadSession.enableReadReceipt(true)
mReadSession.read(100)

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

mReadSession = MesiboReadSession()
mReadSession.initSession(nil, groupid: 0, query: nil, delegate: self)
mReadSession.enableSummary(true)
mReadSession.read(100)

Javascript Example

The way read works in the mesibo Javascript API differs slightly from Android/iOS APIs. This is mainly because of how the local database(IndexDB) operates. In Android/iOS read messages are received in the onMessage callback. In Javascript, you get the number of messages through the read handler that you set while creating the read session.

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 messageSession = api.readDbSession('testuser', 0, null,
    function on_read(count) {
	console.log("Read "+ count + " number of messages");
        var messages = messageSession.getMessages();
});

messageSession.enableReadReceipt(true);
messageSession.read(100);

To read message summary,

var summarySession = api.readDbSession(null, 0, null, 
    function on_read(count) {
	var messages = summarySession.getMessages();
	//Here you will get a message summary which contains a list of each user who was sent/recieved message, their last message, etc.
});
summarySession.enableSummary(true);
summarySession.read(100);

Here is an example in Javascript, to read all messages of a group, with the groupid 98765. Replace it with the groupid you have. Set the peer parameter to null, while reading group messages.

var groupSession = api.readDbSession(null, 98765, null,
    function on_read(count) {
        var messages = groupSession.getMessages();
});

groupSession.enableReadReceipt(true);
groupSession.read(100);

Sending media and files >>

mesibo, android, ios