Write your First mesibo Enabled Application - C++

Estimated reading time: 12 minutes

The mesibo C++ SDK allows you to create a real-time application on Linux, Mac OS, Microsoft Windows, 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 C++. 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/libmesibo.dylib/mesibo.dll) 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;
	}

	void logMessage(MesiboMessage *msg, const char *func) {
		MesiboDateTime *mt = msg->getTimestamp();
		if(msg->isRichMessage())
			ERRORLOG("===> %s: title %s message %s status %d (date: %s) (time: %s)\n", func, msg->getTitle(), msg->getMessage(), msg->getStatus(), mt->getDate(1), mt->getTime(1));
		else {
			MesiboData data;
			msg->getData(&data);
			ERRORLOG("===> %s: %.*s status: %d (date: %s) (time: %s)\n", func, data.len, data.data, msg->getStatus(), mt->getDate(1), mt->getTime(1));
		}
	}

	int Mesibo_onMessage(MesiboMessage *msg) {
		logMessage(msg, "Mesibo_onMessage");
		return 1;
	}
	
	int Mesibo_onMessageUpdate(MesiboMessage *msg) {
		logMessage(msg, "Mesibo_onMessageUpdate");
		return 1;
	}
	
	int Mesibo_onMessageStatus(MesiboMessage *msg) {
		logMessage(msg, "Mesibo_onMessageStatus");
		
		MesiboDateTime *dt = msg->getDeliveryTimestamp(NULL);
		MesiboDateTime *rt = msg->getReadTimestamp(NULL);
		if(dt) ERRORLOG("===> Delivery (date: %s) (time: %s)\n", dt->getDate(1), dt->getTime(1));
		if(rt) ERRORLOG("===> Read (date: %s) (time: %s)\n", rt->getDate(1), rt->getTime(1));
		return 1;
	}
	
	int Mesibo_onConnectionStatus(int status) {
		ERRORLOG("===> Mesibo_onConnectionStatus: %u\n", status);
		return 0;
	}

	int Mesibo_onPresence(MesiboPresence *p) {
		if(p->isTyping())
			ERRORLOG("===> Mesibo_onPresence: is typing\n");
		else if(p->isTypingCleared())
			ERRORLOG("===> Mesibo_onPresence: not typing\n");
		else if(p->isOnline())
			ERRORLOG("===> Mesibo_onPresence: has come online\n");
		else if(p->isOffline())
			ERRORLOG("===> Mesibo_onPresence: is offline\n");
		else if(p->hasJoined())
			ERRORLOG("===> Mesibo_onPresence: has Joined\n");
		else if(p->hasLeft())
			ERRORLOG("===> Mesibo_onPresence: has Left\n");
		else
			ERRORLOG("===> Mesibo_onPresence: %x\n", p->presence);

		return 0;
	}

	void Mesibo_onEndToEndEncryption(const char *address, int status) {
	}
};

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,

MesiboMessage *m = m_api->newMessage("123");
m->setMessage("Hello from mesibo CPP library");
	
fprintf(stderr, "sending\n");
m->send();

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.

MesiboMessage *m = m_api->newMessage(1234);
m->setMessage("Hello from mesibo CPP library");
	
fprintf(stderr, "sending\n");
m->send();

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