Write your First mesibo Enabled Application - Python

Estimated reading time: 4 minutes

In this part, we will create a simple real-time app using Python.

Supported Platforms

  • CentOS / RedHat 7.x or above
  • Debian / Ubuntu
  • Mac OS
  • Raspberry Pi


You MUST go through the following prerequisites before you read further.

  • Installed the Mesibo Real-Time Python Library. If not, refer installation instructions here
  • Read the First App Guide

  • Read the Anatomy of a Mesibo Application about using the Mesibo API and listener class

  • Basic knowledge of writing and running Python code

First Python App

Now let’s quickly start coding:

From the mesibo python package import the function class Mesibo and the callback class MesiboNotify

from mesibo import Mesibo
from mesibo import MesiboNotify

Now, initialize mesibo like shown below

#Initialisation code

#Get auth token and app id from console 
AUTH_TOKEN = "baad7b35749832539002bbff9936130a42aaadd7b2cb0a3e664eabc"
APP_ID = "mypythonapp"

#Create a Mesibo Instance
pymesibo = Mesibo() 

#Set Listener
#Set your AUTH_TOKEN obtained from the Mesibo Console
#Set APP_ID which you used to create AUTH_TOKEN
#Set the name of the database

#Start mesibo

#Wait for the application to exit, press Ctrl+Z to exit

As explained in Anatomy of Mesibo Application, Mesibo invokes a class of Listeners for various events.

Derive from the MesiboNotify class to implement listeners as shown below.

class MesiboListener(MesiboNotify):

    def on_connectionstatus(self, status):
        """A status = 1 means the listener successfully connected to the Mesibo server
        print(f"## Connection status: {status}")
        return 0

    def on_message(self, msg_params, data, datalen):
        """Invoked on receiving a new message or reading database messages
        print(f"## Received message from {msg_params['peer']}: {data} of len {datalen}")
        print("Message: ", data[: datalen].decode('utf-8'))
        return 0

    def on_messagestatus(self, msg_params):
        """Invoked when the status of an outgoing or sent message is changed. Statuses can be
        sent, delivered, or read
        print(f"## Outgoing msg. To: {msg_params['peer']}, Status: {msg_params['status']}")
        return 0

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

Testing your Python application

  1. Run your Python script
python myfirstapp.py
  1. on_connectionstatus 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.

  2. 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 to Message User Enter 1000 (or anything) in the From field, check Create This User checkbox, type message and click on Send.

  1. You will instantly receive this message in your console/terminal in the 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 send_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.

def send_text_message(to, message):
    #pymesibo is the Mesibo Python API instance.
    #Make sure the instance is initialised before you call API functions
    p = {}
    p['peer'] = to
    p['expiry'] = 3600
    data = message
    pymesibo.send_message(p, pymesibo.random(),data)

That’s it! Try it out by creating two users and send messages to each other by using the above function.

Group Messaging >>

mesibo, android, ios, cpp, python