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 to installation instructions here
  • Read the section Create Users to learn about creating an app in mesibo console and creating users & access tokens for your app.
  • 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 the console 
AUTH_TOKEN = "xxxx"
APP_ID = "com.mesibo.python"

# 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

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 send and receive your first real-time message.

Testing your Python application

  1. Run your Python script
    python myfirstapp.py
  2. 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. If you are not getting connection status 1, and getting 4 or 5, you are not passing the correct appid while creating a token. Ensure that you pass the same app id to the set_appname API call.

  3. 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 the Create This User checkbox, type the message and click on Send. 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.

Sending Files >>

mesibo, android, ios, cpp, python