Write your First mesibo Enabled Application - Python
Estimated reading time: 4 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 Python.
Supported Platforms
- CentOS / RedHat 7.x or above
- Debian / Ubuntu
- Mac OS
- Raspberry Pi
Prerequisites
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
pymesibo.set_listener(MesiboListener)
#Set your AUTH_TOKEN obtained from the Mesibo Console
pymesibo.set_accesstoken(AUTH_TOKEN)
#Set APP_ID which you used to create AUTH_TOKEN
pymesibo.set_appname(APP_ID)
#Set the name of the database
pymesibo.set_database("mesibo.db")
#Start mesibo
pymesibo.start()
#Wait for the application to exit, press Ctrl+Z to exit
pymesibo.wait()
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(msg_params)
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
- Run your Python script
python myfirstapp.py
-
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. -
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 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.
mesibo, android, ios, cpp, python