Mesibo Real-time Chat APIs - Initialization

You need to call one or more initialization APIs before using any other mesibo APIs. Following are the various Initialization APIs.

Initialization

This API is for Android only.

This is the first API function you must call before you use any other mesibo APIs.

void init(context);

init takes the following parameters:

ParameterDescription
contextApplication Context

Media and Database Path [Optional]

By default, mesibo automatically chooses the path suitable to your device to store all the files and database. You can change the path using the setPath API. The default Path will be used if this API is not called or the path is not writable. It is recommended to use the default path or check that the path is writable before calling this API.

void setPath(String path);

setPath takes the following parameters:

ParameterDescription
pathValid file system path. Ensure that your app has permission to read/write to this path

Set User Credentials

Set the access token for the user. Refer to the back-end APIs to learn how your backend can generate an access token.

int setAccessToken(String token);

setAccessToken takes the following parameters:

ParameterDescription
tokenUser Access Token Obtained using Backend APIs

The access token must be generated specifically for the particular application using the backend API. The setAccessToken API will fail if the access token was generated for a different app. Refer to the appid described in the token generation section of back-end APIs. You can also use utility function Mesibo.getAppIdForAccessToken() to know about the App ID associated with the app.

Setting up a Database to Store Messages

Enable a local database to store all incoming & outgoing messages, profiles, and other information. If this API is not called, Mesibo will not use any local database to store messages. Note that many APIs depends on the database and they will not work if the database is not set up.

It is recommended to call setDatabase after setting the user access token using setAccessToken API. It ensures a unique local database for each user. If setDatabase is called before setAccessToken API function, the same local database will be used for all the users.

boolean setDatabase(String dbname);

setDatabase takes the following parameters:

ParameterDescription
dbnameName, or the complete path of the database. If the path is not specified, the database will be stored in the default path or the path set by setPath API

Add Listener

Mesibo invokes listeners for various events. For example, when you receive a message, receive an incoming call, connection status, etc. You can implement these listeners to get real-time event notifications. Listeners are described in various sections in the API documentation.

You can add a listener using 'addListener' API. You can add multiple listeners if requires.

void addListener(MesiboListener listener);

addListener takes the following parameters:

ParameterDescription
listenerlistener object (of any type)

Remove Listener

Remove a listener previously added using addListener API.

void removeListener(MesiboListener listener);

removeListener takes the following parameters:

ParameterDescription
listenerlistener object (of any type)

Start Mesibo

Once you initialize, you can start Mesibo. Mesibo will start secure connection establishment with Mesibo cloud servers OR your on-premise Mesibo server. Note that, Mesibo will not establish a network connection till start API is called. However, once the start is called, Mesibo will automatically manage any future reconnections till stop() is called

void start();

start does not take any parameters.

Stop Mesibo

Disconnect any existing connection and also prevent future reconnections.|Void|

void stop();

stop does not take any parameters.

Initialization APIs Example

Below is an example of using initialization APIs.


Complete code on GitHub for Javaopen_in_new

Mesibo api = Mesibo.getInstance();
api.init(getApplicationContext());

// Set the user access token
Mesibo.setAccessToken(token);

Mesibo.addListener(this);

// Set the name of the database
Mesibo.setDatabase("mesibo");

// Start mesibo,
Mesibo.start();

Complete code on GitHub for Kotlinopen_in_new,

val api: Mesibo = Mesibo.getInstance()
api.init(applicationContext)

// Set the user access token
Mesibo.setAccessToken(token)

Mesibo.addListener(this)

// Set the name of the database
Mesibo.setDatabase("mesibo", 0)

// Start mesibo,
Mesibo.start()

Complete code on GitHub for Swiftopen_in_new

// Set the user access token
Mesibo.getInstance().setAccessToken(token)

Mesibo.getInstance().addListener(self)

// Set the name of the database
Mesibo.getInstance().setDatabase("mesibo")

// Start mesibo,
Mesibo.getInstance().start()

Complete code on GitHub for Objective-Copen_in_new

// Set the user access token
[MesiboInstance setAccessToken:token];

[MesiboInstance addListener:self];

// Set the name of the database
[MesiboInstance setDatabase:@"mesibo"];
    
// Start mesibo,
[MesiboInstance start];

Complete code on GitHub for Flutteropen_in_new

Mesibo _mesibo = Mesibo();

// Set the user access token
_mesibo.setAccessToken(token);

_mesibo.setListener(this);

// Set the name of the database
_mesibo.setDatabase("mesibo");

// Start mesibo,
_mesibo.start();

Complete code on GitHub for JavsScriptopen_in_new

var api = new Mesibo();

// Set the user access token
api.setAppName("com.mesibo.firstapp");
api.setAccessToken(token);

api.setListener(listener);

// Set the name of the database
api.setDatabase("mesibo");

// Start mesibo,
api.start();

Complete code on GitHub for Pythonopen_in_new

#!/usr/bin/python3
import mesibo
from mesibo import MesiboListener

# Create a Mesibo Instance
api = mesibo.getInstance()

api.addListener(listener)

# Set the user access token
api.setAppName("com.mesibo.firstapp")

if(mesibo.MESIBO_RESULT_FAIL == api.setAccessToken(ACCESS_TOKEN)):
    print("===> Invalid ACCESS_TOKEN: ", ACCESS_TOKEN)
    print("See https://docs.mesibo.com/tutorials/get-started/")
    exit(1)

# Set the name of the database
api.setDatabase("mesibo", 0)

# Start mesibo,
api.start()

Complete code on GitHub for C++open_in_new

#include <mesibo.h>

Mesibo *m_api = MesiboInstance(0);

// Set the user access token
m_api->setAppName(APP_ID);
m_api->setAccessToken(APP_TOKEN);
	
m_api->addListener(listener);

// setup database to store incoming messages
if(0 != m_api->setDatabase("mesibo.db", 0)) {
	return -1;
}

// start mesibo
m_api->start();