Get Started: Reading Messages

Estimated reading time: 7 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- text, file(like image, video, etc) or binary data. Any mesibo application that you build there are three kinds of storages that you need to handle on the whole:

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

  • Mesibo Storage Information regarding the users of your application, their login status, queued messages, their profiles, groups, etc will be stored on a database by mesibo. If you are hosting mesibo on your servers-mesibo on-premise you will be managing this database on your own. Else if you are using mesibo cloud services, mesibo will store and handle the data in cloud storage.

  • File Storage Mesibo does not mandate where you store your files. You can host your files on AWS, Azure, or any such cloud hosts or your private file servers. The files uploaded by your users will be stored on your file server with your backend by communicating with your file server. When a user needs to download a file, you need to fetch it from your file servers. To do this you need to write file handlers that generate a file URL for each of your files, which you can store in the user’s database.

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, 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 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

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

Android Example

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);

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

In Objective-C,

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

Javascript Example

var summarySession = api.readDbSession(null, 0, null, 
    function on_messages(m) {
        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 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_messages(m) {
        var messages = messageSession.getMessages();
});

messageSession.enableReadReceipt(true);
messageSession.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_messages(m) {
        var messages = groupSession.getMessages();
});

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

Sending media and files >>

mesibo, android, ios