Mesibo Real-time Chat APIs - Messaging
Estimated reading time: 9 minutesIn this section, we will describe how to send messages.
Basic Usage
To send messages, you only need to create a MesiboMessage
object for the destination, add your content (text message, files, location, etc.), set message properties if requires, and call send
method. You can create a MesiboMessage
object by calling
Create
MesiboMessage message = profile.newMessage();
OR,
MesiboMessage message = new MesiboMessage(profile);
where the profile
is the user or group profile to whom you want to send a message. Once you create a MesiboMessage
object, all you need to do is to add your message content and call send()
. There is no difference between how you send messages to an individual user or a group, the destination is set based on the profile you have used to create MesiboMessage
.
MesiboMessage msg = profile.newMessage();
msg.title = "Mesibo is awesome";
msg.message = "My First Mesibo Message";
msg.send();
You can also add a file using file path, URL, or Android Bitmap as required. For example,
msg.setContent("/file/path");
msg.send();
You can also geotag your messages by adding location information,
msg.latitude = 37.4275;
msg.longitude = 122.1697;
msg.send();
In addition to message content, there are various messaging properties which are set by default when you create a MesiboMessage
object, for example, message ID, expiry of message, message status etc. You can modify them if requires, as we have described in this document.
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.
MesiboMessage
Messaging APIs
send
Send a message.
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);