Mesibo Real-time Chat APIs - Initialization

Estimated reading time: 5 minutes

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:

Parameter Description
context Application 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:

Parameter Description
path Valid 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:

Parameter Description
token User Access Token Obtained using Backend APIs

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, boolean resetTables);

setDatabase takes the following parameters:

Parameter Description
dbname Name, 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
resetTables reset existing tables

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:

Parameter Description
listener listener object (of any type)

Remove Listener

Remove a listener previously added using addListener API.

void removeListener(MesiboListener listener);

removeListener takes the following parameters:

Parameter Description
listener listener object (of any type)

Set Secure Connection [Depreciated]

Enable secured encrypted connection.

This API is depreciated. Encryption is always enabled now.

void setSecureConnection(boolean enable);

setSecureConnection takes the following parameters:

Parameter Description
enable enable or disable encryption

Start Mesibo

Once you initialize, you can start Mesibo. Mesibo will start 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

start does not take any parameters.

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.

Android Example

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

// add listener
Mesibo.addListener(this);

// set access token
if(0 != Mesibo.setAccessToken(accessToken) {
	return false;
}

// set database after setting access token so that it's associated with the user
Mesibo.setDatabase("mesibo.db", 0);

// Now start mesibo
if(0 != Mesibo.start()) {
	return false;
}

iOS Example

In Swift

Mesibo.getInstance().addListener(self)

Mesibo.getInstance().setAccessToken(token)
Mesibo.getInstance().setDatabase("mesibo.db", resetTables: 0)
Mesibo.getInstance().start()

In Objective-C

[MesiboInstance addListener:self];

[MesiboInstance setAccessToken:accessToken];
[MesiboInstance setDatabase:@"mesibo.db"] ; 

[MesiboInstance start];

JavaScript Example

var api = new Mesibo();

api.addListener(listener);
api.setAppName(app_id);
api.setAccessToken(token);
api.setDatabase("mesibo");

api.start();
real-time chat API, initialize guide for api