Mesibo Real-time Chat APIs - Messaging

Estimated reading time: 7 minutes

Sending messages using Mesibo only requires initializing a MesssageParams object with needed parameters and invoke one of the messaging API with params, Message-ID, and the message.

Each message has essentially two parts:

  • Message properties, for example, type of message, expiry, Message-ID, etc. Refer to Getting Started Guide for more information. You can specify message properties using MessageParams
  • Message

Message-ID

You should use a unique Message-ID for sending each message so that you can identify the message when the message status is received. Message-ID is a 32-bit unique id. It will be converted into a 64-bit globally unique ID by the system. The ID which you have originally passed will be preserved in the lower 32-bit of the globally unique ID.

For example, if you send a message with id 0x12345678, it will be converted into global id 0xXXXXXXXX12345678 when delivering your message to the receiver. If you use zero Message-ID, the message will be delivered only if the recipient is online, it will be discarded otherwise.

You should use zero message-ID only for cases where storage is not required, for example, presence information like typing indicators, online status, etc.

Messaging APIs

sendMessage

Send a message. There are overloaded functions to send messages in different formats, for example, string, byte array, etc.

int sendMessage(MesiboParams params, long mid, String message);
int sendMessage(MesiboParams params, long mid, bytes[] data);

sendMessage takes the following parameters:

Parameter Description
params Message properties (MessageParams) object initialized with required parameters
mid Message-ID for sending messages. Message-ID should be passed in the API parameters instead of setting it in params. This enables using the same MessageParams object from different threads
message Message (Unicode String)
data data (Binary data)

sendFile

Send a file. This API is described in more detail in the File Transfer API. Note that this API is deprecated and will soon be replaced by a common API.

int sendFile(MesiboParams params, long mid, MesiboFileInfo file);

sendFile takes the following parameters:

Parameter Description
params Message properties (MessageParams) object initialized with required parameters
mid Message-ID for sending messages. Message-ID should be passed in the API parameters instead of setting it in params. This enables using the same MessageParams object from different threads
file FileInfo object initialized with file information like path, type, etc.

sendLocation

Send a location. This API is deprecated and will soon be replaced by a common API.

int sendLocation(MesiboParams params, long mid, MesiboLocation location);

sendLocation takes the following parameters:

Parameter Description
params Message properties (MessageParams) object initialized with required parameters
mid Message-ID for sending messages. Message-ID should be passed in the API parameters instead of setting it in params. This enables using the same MessageParams object from different threads
location MesiboLocation object initialized with location information like latitude, longitude, address, etc.

forwardMessage

Forward an existing message to a user or a group. A FORWARD flag will be set at the recipient end to indicate it as a forwarded message.

int forwardMessage(MesiboParams params, long mid, long forwardid);
int forwardMessage(MesiboParams params, long mid, long forwardid, boolean markForwarded);

forwardMessage takes the following parameters:

Parameter Description
params Message properties (MessageParams) object initialized with required parameters
mid Message-ID for sending messages. Message-ID should be passed in the API parameters instead of setting it in params. This enables using the same MessageParams object from different threads
forwardid Message-ID of an existing message to be forwarded
markForwarded Mark message as the forwarded message (default true)

You must enable local database using setDatabase API for this API to work.

resendMessage

Resend a failed message.

boolean resend(long mid);

resendMessage takes the following parameters:

Parameter Description
mid Message-ID of an existing message to be resent

You must enable local database using setDatabase API for this API to work.

cancel

Cancel a message. Message can not be canceled if already sent & delivered. Use deleteMessage API if you like to recall a delivered message.

int cancel(int type, long mid)

cancel takes the following parameters:

Parameter Description
type Type of cancellation
mid Message-ID of an existing message to be cancel

deleteMessage

Delete a message.

boolean deleteMessage(long mid);

deleteMessage takes the following parameters:

Parameter Description
mid Message-ID of an existing message to be deleted

deleteMessages

Delete messages matching criteria in MessageParams object. For example, if you pass MessageParams with address set, it will delete all the messages from an address

boolean deleteMessages(MesiboParams params);

deleteMessages takes the following parameters:

Parameter Description
params Message properties (MessageParams) object initialized with required parameters

random

Returns a new unique message ID that you can use to send messages.

long randomn();

random does not take any parameters.

Messaging APIs Example

Android Example

In Java,

Mesibo.MessageParams p = new Mesibo.MessageParams(to, Mesibo.FLAG_DEFAULT);

int rv = Mesibo.sendMessage(p, Mesibo.random(), "A Test Message");
if(Mesibo.RESULT_OK == rv) {
	Log.d(TAG, "Message sent");
} else {
	Log.d(TAG, "Message failed: " + rv);
}

In Kotlin,

val p: Mesibo.MessageParams = Mesibo.MessageParams()
p.peer = mRemoteUser!!.address
p.flag = Mesibo.FLAG_READRECEIPT or Mesibo.FLAG_DELIVERYRECEIPT
read-session-apis-exampleMesibo.sendMessage(p, Mesibo.random(), "A Test Message");

iOS Example

In Objective-C,

MesiboParams *p = [MesiboParams new];

[p setPeer:to];
[p setFlag:MESIBO_FLAG_READRECEIPT|MESIBO_FLAG_DELIVERYRECEIPT];
[p setExpiry:3600*24*7];

uint32_t mid = [MesiboInstance random];
[MesiboInstance sendMessage:params msgid:mid string:@"A Test Message"];

In Swift,

let params = MesiboParams()
params.peer = mRemoteUser
params.flag = (UInt32)(MESIBO_FLAG_READRECEIPT | MESIBO_FLAG_DELIVERYRECEIPT)

let mid = Mesibo.getInstance()!.random()
Mesibo.getInstance()!.sendMessage(params, msgid: mid, string: "A Test Messages")

Javascript Example

var p = {};
p.peer = to;
p.flag = MESIBO_FLAG_DEFAULT;
api.sendMessage(p, api.random(), message);
messaging api, android messaging api, real-time chat api, messaging api for ios