Write your First mesibo Enabled Application - C/C++
Estimated reading time: 6 minutes- Basic Concepts
- Real-Time Messaging
- Anatomy
- First App
- Android
- iOS
- Javascript
- C++
- Python
- Group Messaging
- Reading Messages
- Sync Messages
- Sending Media & Files
- Push Notification
- Voice & Video Calls
- Conferencing
- UI Modules
In this part, we will create a simple real-time app using Mesibo C/C++ API. The Mesibo C/C++ SDK allows you to create a real-time application on Linux
, Mac OS
, and Raspberry Pi
platforms. Mesibo C/C++ API is the low-level API that is extremely powerful, efficient, and light-weight.
Mesibo C/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/C++ libraries.
Supported Platforms
- CentOS / RedHat 7.x or above
- Debian / Ubuntu
- Mac OS
- Raspberry Pi
Prerequisites
-
Installed the Mesibo Real-Time C/C++ Library. If not, refer installation instructions here
-
Read the First App Guide
-
Read the Anatomy of a Mesibo Application
-
Basic knowledge of writing and compiling C/C++ code
First C/C++ App
Now let’s quickly start coding:
- Open a code editor
- Include
mesibo.h
in your code - Create a class derived from the
CMesiboNotify
class to listen to all the message and call events. - Initialize mesibo using the user token and listener.
#include <mesibo.h>
Below is Mesibo Event 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;
}
};
Following is the Mesibo Initialization code.
#define AUTH_TOKEN 3e7694e19d192588a4ffcb4eab26b6afb3d5aada54bbd41ed140a
#define APP_ID "mycppapp"
IMesibo* mesibo_init(){
// Create a Mesibo Instance
m_api = query_mesibo("/tmp");
// Add Listener
CNotify *n = new CNotify();
m_api->set_notify(0, n, 1);
// Set your AUTH_TOKEN obtained from the Mesibo Console
m_api->set_credentials("3e7694e19d192588a4ffcb4eab26b6afb3d5aada54bbd41edd7140a");
// Set APP_ID which you used to create AUTH_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;
}
return m_api;
}
Now, after initialization is complete you run Mesibo
m_api->start();
That’s it - you are now ready to receive your first real-time message.
Compiling and Running sample C/C++ application
It is recommended that you use a modern C/C++ compiler such as GCC (GCC-C++ 4.x or above) or clang. You can compile your code by linking the Mesibo Library as shown below:
g++ my_first_app.cpp -o myfirstapp -lmesibo
Run the output executable
./myfirstapp
-
on_status
should cycle through various status information. Finally, you should receive status=1 which indicates that your app is successfully connected to the mesibo real-time server and ready to send and receive real-time messages. -
Since we do not have any other users right now, we will use mesibo console to send a test message. In a later section, we will learn how to send messages from the code itself.
-
Go to Console ->Application->Users. You should see the user you have created.
-
Go to user details by clicking the
Edit
button. Scroll down, you will see a section toMessage User
Enter 1000 (or anything) in theFrom
field, checkCreate This User
checkbox, type message and click onSend
.
- You will instantly receive this message in your console/terminal in an
on_message
listener.
Sending Messages
In the previous section, we have used the mesibo console to send a message. Now we will quickly learn how to send messages from the code itself. 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 = {};
p.id = m_api->random32();
p.expiry = 3600;
int datalen = strlen(message);
m_api->message(&p, to, message, datalen);
}
That’s it! Try it out by creating two users and send messages to each other by using the above function.
First Mesibo Application - Python >>
mesibo, android, ios, cpp, c++, c