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 app for C++.

The app will allow you to log in as two different users so that you can try various mesibo features. We will cover the following in this part.

Please go through the following prerequisites before you read further.

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. If not, refer installation instructions here. 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.
    • CentOS / RedHat 7.x or above
    • Debian / Ubuntu
    • Mac OS (In Progress)
    • Raspberry Pi (In Progress)
  • Basic knowledge of writing and compiling C++ code

Configure and Build First C++ App

Now let’s quickly start coding:

  1. Open a code editor
  2. Include mesibo.h in your code
  3. Create a class derived from the CMesiboNotify 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"

int main(){
	
	// Create a Mesibo Instance
	m_api = query_mesibo("/tmp");

	// Add Listener
	CNotify *n = new CNotify();
	m_api->set_notify(0, n, 1);
	// Set your ACCESS_TOKEN obtained from the mesibo Console
	m_api->set_credentials(ACCESS_TOKEN);
  	// Set  APP_ID which you used to create ACCESS_TOKEN 
	m_api->set_device(1, "cpp", APP_ID, "1.0.0");
	// Set the name of the database
	if (0 != m_api->set_database("mesibo.db")) {
		fprintf(stderr, "Database failed\n");
		return NULL;
	}
	
  m_api->start();
  
  m_api->wait();
	
  return 0;
}

Implement mesibo Listener

class CNotify: public CMesiboNotify {
	
	IMesibo *m_api;
	public:
	
	void set_api(IMesibo *api) {
		m_api = api;
	}


	// You will receive the connection status here
	int on_status(int status, uint32_t substatus, uint8_t channel,
			const char *from) {
		ERRORLOG("===> on_status: %u \n", status);
		return 0;
	}


	// Invoked on receiving a new message or reading database messages
	// You will receive messages here.
	int on_message(tMessageParams * p, const char *from, const char *data,
			uint32_t len) {
	
		int printlen = len;
	        if (printlen > 64) printlen = 64;

		ERRORLOG("===> message received:from %s,of len %d: %.*s\n",
			from, len, printlen, data);

		return 0;

	}

	// Invoked when the status of the outgoing or sent message is changed
	// You will receive the status of sent messages here
	int on_messagestatus(tMessageParams * p, const char *from, int last) {
		ERRORLOG(
			"===> on_messagestatus status %u from: %s\n",p->status, from);
		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 message real-time API for which we will need destination user, message-id, and the message itself. Invoke the following function anywhere from your code to send a text message.

int send_text_message(const char* to,const char * message){
  tMessageParams p;
  memset(&p, 0 ,sizeof(p));
  p.id = m_api->random32();
  p.flag = MESIBO_FLAG_DEFAULT;
  p.expiry = 3600;
  int datalen = strlen(message);
  m_api->message(&p, to, message, datalen);
}

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 group message.

tMessageParams p;
memset(&p, 0 ,sizeof(p));
p.id = m_api->random32();
p.flag = MESIBO_FLAG_DEFAULT;
p.groupid = 104719;
p.expiry = 3600;
int datalen = strlen(message);
m_api->message(&p, NULL, message, datalen);

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

mesibo provides a set of APIs deal with stored messages 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 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
void* read_session = m_api->set_readsession(NULL, MESIBO_READFLAG_READRECEIPT, user_address, 0, "");
int count = m_api->read(read_session, 100);

To read messages for a group,

int groupid = 96876 //Example
void* group_read_session = m_api->set_readsession(NULL, MESIBO_READFLAG_READRECEIPT, NULL, groupid, "");
int group_read_count = m_api->read(read_session, 100);

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

void* summary_read_session = m_api->set_readsession(NULL, MESIBO_READFLAG_SUMMARY, NULL, 0, "");
int summary_read_count = m_api->read(read_session, 100);

First mesibo Application - Python >>

mesibo, get-started, tutorial, c++