Hosting Server-side APIs for Messenger App
In the first part of this tutorial, we built and ran the Messenger app without setting up any server-side APIs of our own. That’s because the Messenger app uses private backend APIs hosted on mesibo by default, allowing you to get started quickly.
However, when you develop your own production application, you will need to integrate your application’s authentication mechanism with mesibo. This is where Messenger Private APIs come in.
These are the backend APIs used by the Messenger apps to authenticate users. You can host these APIs on your own infrastructure and customize them to work with your existing user database and authentication system.
Your authentication mechanism is entirely up to you. You can use phone numbers and OTPs, email and passwords, OAuth, or any other method you prefer.
How Messenger Private APIs Work
The Messenger app uses two types of APIs:
- Messenger Private APIs: These handle user authentication and registration. They run on your backend and communicate with the mesibo Backend API to generate access tokens for authenticated users.
- mesibo Real-Time APIs: These are used by the Messenger app to send and receive messages, make voice and video calls, and perform other real-time communication operations.
Let’s briefly understand how authentication works:
Every user who connects to mesibo needs a mesibo access token. This token identifies and authenticates the user with the mesibo real-time server, allowing them to send messages, make calls, and use other mesibo features.
The token is generated using the mesibo Backend API, a set of REST APIs that your private backend can invoke.
When a user signs up or logs in, the Messenger app sends the user’s credentials to your private backend. Your backend verifies the user using your chosen authentication mechanism, such as a username and password or an OTP.
If authentication succeeds, your private backend calls the mesibo Backend API to create the user’s mesibo access token. It then returns the token to the Messenger app.
The Messenger app uses this token to initialize the mesibo Real-Time API (Android, iOS or Web) and start communicating through mesibo.
The following diagrams illustrate the authentication and token flow.

Refer to the Getting Started guide for more details about mesibo authentication and access tokens.
The authentication step is also where you can integrate your own verification process. For example, you can use a commercial SMS gateway to send an OTP to the user’s phone number instead of relying on the demo OTP mechanism.
Understanding the Login API Flow
Let’s look at the actual message exchange between the Messenger app and its private backend.
For testing, the default Messenger Private API is hosted at:
https://messenger.mesibo.com
When a user signs up in the Messenger app, the app sends a request to the private API with the following payload:
{
"op": "login",
"phone": "18005551234",
"appid": "com.messenger.appid"
}
Here, appid identifies the application (for example, the Android package name or iOS Bundle ID), and phone contains the user’s phone number.
At this stage, the private API can generate and send an OTP to the user’s phone. It responds with:
{
"op": "login",
"ts": 1592890269,
"result": true
}
The Messenger app uses this response to proceed to the OTP verification screen.
Once the user receives and enters the OTP, the app invokes the same API again, this time including the OTP:
{
"op": "login",
"phone": "18005551234",
"appid": "com.messenger.appid",
"otp": "123456"
}
If authentication succeeds, the private backend calls the mesibo Backend API to create an access token for the user. It then returns the token to the Messenger app:
{
"op": "login",
"result": true,
"token": "YOUR_MESIBO_USER_ACCESS_TOKEN"
}
The Messenger app uses this access token to initialize the mesibo Real-Time API and connect to the mesibo server.
The important point is that mesibo does not dictate how your application authenticates its users. The private backend is responsible for verifying the user’s identity. You can implement this using your own database and authentication system, or integrate an existing identity provider.
Now let’s see how to host the sample private APIs on your own server.
Hosting Messenger Private APIs
The Messenger Private APIs are written in PHP. You can download the source code from the messenger-app-backend GitHub repository.
Clone the repository:
git clone https://github.com/mesibo/messenger-app-backend.git
You can now review and customize the PHP implementation to suit your application.
Consider starring the project on GitHub to follow future updates.
Configuring the Private API
Before hosting the API, you need to configure your mesibo App Token.
- Log in to the mesibo Console.
- Open the app you created in the first part of this tutorial.
- Go to App Settings and locate the App Token.
- Configure this token in
mesiboconfig.phpin the backend source code.
Important: The mesibo App Token is a secret credential. It must remain on your server and must never be included in your Android, iOS or Web application.
That’s it! You can now host the private API on your server and test the login flow.
For example, use curl, Postman or another HTTP client to send a login request to your hosted API.
curl --header "Content-Type: application/json" \
--request POST \
--data '{"op":"login","phone":"18005551234","appid":"com.messenger.appid"}' \
https://example.com
A successful initial login request should return a response similar to:
{
"op": "login",
"ts": 1592890269,
"result": true
}
This indicates that the backend is responding to the login request. You should also test the complete OTP verification flow to confirm that authentication and token generation work correctly.
Sending OTPs to Your Users
The sample private API generates an OTP and writes it to the PHP error.log file. This is useful for testing, but it is not suitable for a production deployment.
For a real application, you need to deliver the OTP to the user so they can enter it on the login screen.
To do this, modify the sendOtpToUser function in the backend and integrate your preferred SMS provider.
Refer to the sendOtpToUser function in the private API source code.
function sendOtpToUser($phone, $otp) {
// Add your code here to send the OTP
// using your preferred SMS provider.
}
You can use any SMS gateway or verification service that suits your application.
Push Notifications
The next step is to configure push notifications for Android and iOS.
You will need the relevant push notification credentials for your platforms:
- Android: Google Firebase Cloud Messaging (FCM) credentials.
- iOS: Apple Push Notification service (APNs) credentials.
Once you have these credentials, configure them in the mesibo Console by opening your app and navigating to the Push Notifications tab.
Refer to the mesibo Push Notification documentation for details.
The next step is to configure the Android, iOS and Web Messenger apps to use your newly hosted private API.
Configure Apps to Use Your Private API
Finally, update the Messenger apps to use your newly hosted private API instead of the default endpoint:
https://messenger.mesibo.com
The configuration location differs by platform.
Android
- Edit
app/src/main/res/values/strings.xmland enter your Google Maps API key in theGOOGLE_MAP_KEYfield. - Edit
SampleApi.javaand changemApiUrlto point to your private API URL.
iOS
- Open
Info.plistand enter your Google Maps API key in theGoogleMapKeyfield. - In the same file, update
MessengerApiUrlwith your private API URL.
JavaScript
- Edit
mesibo/config.jsand update the API URL to point to your private backend.
After making these changes, rebuild or reload the apps and test the login flow using your hosted backend.
Using a Different Authentication Mechanism
The Messenger sample uses phone-number authentication with OTP verification. This is only an example; mesibo does not require you to use phone-based authentication.
You can replace it with any authentication mechanism supported by your application, such as:
- Email and password
- User ID and password
- OAuth 2.0
- LDAP or RADIUS
- Kerberos or SAML
- Your own application-specific authentication system
The key requirement is that your private backend must authenticate the user first and then generate a mesibo access token using your secret App Token.
In most cases, you only need to modify two parts of the Messenger application:
Login screens: Update the login UI so users can enter the credentials required by your authentication system, such as an email address, user ID or password.
Private Backend APIs: Modify the backend authentication logic to validate those credentials. For example, you can check the user’s credentials against your own database or delegate authentication to an identity provider.
Once authentication succeeds, your backend can create the mesibo access token and return it to the app. The rest of the mesibo real-time communication flow remains the same.
Conclusion
In this part, we learned how the Messenger Private APIs work, how to host them on our own infrastructure, and how to configure the Messenger apps to use our backend.
You can now replace the sample authentication mechanism with your own and integrate mesibo into your application’s existing user management system.
In the next part, we’ll explore how to host the mesibo real-time server on your own infrastructure.