Write your First mesibo Enabled Application - C++

Estimated reading time: 11 minutes

The mesibo C++ SDK allows you to create a real-time application on Linux, Mac OS, and Raspberry Pi platforms. mesibo C++ API is a low-level API that is extremely powerful, efficient, and light-weight.

In this part, we will create a simple real-time messaging and calling app for Android. This app will let you log in as two different users so that you can try various mesibo features.

We will quickly explore the following

  • Sending and Receiving Messages

  • Group Messaging

  • Reading Messages from database

Prerequisites and Download Source Code

  • Read the section Create Users to learn about creating an app in mesibo console and creating users & access tokens for your app.

  • Installed the mesibo Real-Time C++ Library for your platform. mesibo C++ library is available as a shared library(libmesibo.so) which can be seamlessly integrated into any application by compile-time linking or by loading it dynamically. You can even use it from other languages of your choice like Python, PHP, Matlab, etc. as all of these languages allow interfacing with C++ libraries.

  • Basic knowledge of writing and compiling C++ code

Configure and Build First C++ App

Now let’s quickly start coding. You can download entire sample code here https://github.com/mesibo/libmesibo/blob/master/examples/mesibo.cpp

  1. Open a code editor
  2. Include mesibo.h in your code
  3. Create a class derived from the MesiboListener class to listen to all the message and call events.
  4. Initialize mesibo using the user token and listener.
#include <mesibo.h>

Code Explanation

Initialize mesibo

Before you send and receive real-time messages and calls, you need to initialize mesibo. The initialization involves the following steps:

  1. Use setAccessToken to set the user’s access token that you obtained while creating the user
  2. Set up mesibo to invoke your listener class using addListener
  3. Set up database to store sent and received messages using setDatabase (Optional)
  4. Start mesibo

Note that, except addListener, all other initialization functions should be only called once.

Now, initialize mesibo like shown below

// Get access token and app id by creating a mesibo user
// See https://mesibo.com/documentation/tutorials/get-started/
#define ACCESS_TOKEN "xxxx" 
#define APP_ID "xxxxx"

	
// get mesibo API instance (singleton)
Mesibo *m_api = MesiboInstance(0);

// setup credentials
m_api->setAppName(APP_ID);
m_api->setAccessToken(APP_TOKEN);

// create a listener instance to receive incoming messages
SampleListener *n = new SampleListener();
n->set_api(m_api); // Optional, in case listener need to access APIs
m_api->addListener(n);

// setup database to store incoming messages
if(0 != m_api->setDatabase("mesibo.db", 0)) {
	fprintf(stderr, "Database failed\n");
	return -1;
}

// start mesibo
m_api->start();

Implement mesibo Listener

class SampleListener: public MesiboListener  {
	Mesibo *m_api;
	public:
	void set_api(Mesibo *api) {
		m_api = api;
	}

	int Mesibo_onMessage(MesiboMessageParams *p, const char *data, uint32_t len) {
		ERRORLOG("===> Mesibo_onMessage: uid %u status %d type %u id %" PRIx64 " refid %lu groupid %u, when %" PRIu64 " from %s, flag: %x len %d: %.*s\n", p->uid, p->status, p->type, p->mid, p->refid, p->groupid, p->ts, p->peer, p->flags, len, len, data);

		return 1;
	}
	
	int Mesibo_onRichMessage(MesiboMessageParams *p, MesiboRichMessage *m) {
		ERRORLOG("file url %s size %u lat %f long %f\n", m->file.url, m->file.size, m->location.lat_d, m->location.lon_d); 
		return 0;
	}

	int Mesibo_onMessageStatus(MesiboMessageParams *p)  {
		ERRORLOG("===> Mesibo_onMessageStatus status %u id %u when %u ms (%u %u) from: %s\n", p->status, p->mid, m_api->getTimestamp()-p->ts, m_api->getTimestamp(), p->ts, p->peer?p->peer:"");
		return 0;
	}

	int Mesibo_onConnectionStatus(int status) {
		ERRORLOG("===> Mesibo_onConnectionStatus: %u\n", status);
		return 0;
	}

	int Mesibo_onActivity(MesiboMessageParams *p, uint32_t activity, uint32_t value) {
		ERRORLOG("===> Mesibo_onActivity: %x\n", activity);
		return 0;
	}
};

That’s it - you are now ready to send and receive your first real-time message.

Sending and Receiving Messages

Now we will quickly learn how to send messages in real-time.

To send messages, we will use the sendMessage real-time API for which we will need destination user, message-id, and the message itself. For example,

MesiboMessageParams params;
memset(&params, 0, sizeof(MesiboMessageParams));
params.peer = "12345678";
params.expiry = 3600;
params.flags = MESIBO_FLAG_DEFAULT;

const char *message = "Hello from mesibo CPP library";

int rv = m_api->sendMessage(&params, m_api->random(), message, strlen(message));

Once you send a message, the recipient will receive the message through Mesibo_onMessage. mesibo will inform the sender of the message about the status of the message - sent, delivered, or read through the listener Mesibo_onMessageStatus.

Group Messaging

Group messaging is no different from the one-to-one messaging. You can invoke the same messaging APIs to send a group message, instead of the user’s profile, you need to use group profile.

For example, here is an example of how you can send a message to a group. Let’s say you have a group with the group-id 96568. Now, you use the same function that you use for sending a one-to-one message, but instead of using the user profile, you first get the group profile using the group-id and use it to send a group message.

MesiboMessageParams params;
memset(&params, 0, sizeof(MesiboMessageParams));
params.groupid = 123;
params.expiry = 3600;
params.flags = MESIBO_FLAG_DEFAULT;

const char *message = "Hello from mesibo CPP library";

int rv = m_api->sendMessage(&params, m_api->random(), message, strlen(message));

A message sent will be delivered to all members of the group and you will get the message delivery and read status for each user.

Creating a Group

To create a group and add members, you need to use the Group Management APIs. This is the recommended approach. However, you can also create a group and add/remove members by using backend APIs and mesibo console if requires.

You can create a new group by calling createGroup API, with name and the Group Listener Object.

It will send a request to the server to create a new group. When a group is created, callback function in the listener will be called with the group profile. You can then add members to the group using group profile APIs.

You can now use this new group profile for messaging, adding more members or changing group name, description, picture, etc.

Adding members to a Group

You can add members, and admins to the group by calling addMembers API of the group profile. You need to pass the addresses of the users to be added as group members, permissions to be granted and admin permissions if you are adding them as group admins.

For example,

Reading Messages & Call History

mesibo provides a set of APIs to read messages and call history stored in the database and sending read receipts. A read receipt is an acknowledgment that the user has read the message.

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

To read messages for a user,

const char* user_address = "456"; //Example
MesiboReadSession* read_session = m_api->createReadsession(user_address, 0, NULL, this);
read_session->enableReadReceipt(1);
int count = read_session->read(100);

To read messages for a group,

int groupid = 96876 //Example
MesiboReadSession* read_session = m_api->createReadsession(NULL, groupid, NULL, this);
int count = read_session->read(100);

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

MesiboReadSession* read_session = m_api->createReadsession(NULL, 0, NULL, this);
read_session->enableSummary(1);
int count = read_session->read(100);

First mesibo Application - Python >>

mesibo, get-started, tutorial, c++