Get Started: Reading Messages

Estimated reading time: 9 minutes

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


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

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

Mesibo.ReadDbSession mReadSession = new Mesibo.ReadDbSession(null, 0, null, this);

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)

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

mReadSession = Mesibo.ReadDbSession(null, this)

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)

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

mReadSession = MesiboReadSession()
mReadSession.initSession(nil, groupid: 0, query: nil, delegate: self)

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


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.

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


Sending media and files >>

mesibo, android, ios