Getting Started Guide
Sending and Receiving Messages
In this section, we will describe how to send and receive messages.
mesibo allows you to send and receive various types of real-time messages - plain text, rich message with media such as image, audio, video, doc, etc, URL preview, location, binary data, etc.
A message has various properties, for example, id, status, expiry, etc. Refer to the messaging api documentation for more details.
Sending Messages
To send messages, you only need to create a MesiboMessage
object using the destination profile, which could be user profile or a group profile.
Note that, group messaging is no different from the one-to-one messaging. You can invoke the same messaging APIs to send a group message, instead of the user's profile, you need to use group profile. A message sent will be delivered to all members of the group and you will get the message delivery and read status for each user.
Send a Plain Text Message
MesiboMessage msg = profile.newMessage();
msg.message = "My First Mesibo Message";
msg.send();
where the profile
is the user or group profile to whom you want to send a message. 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
.
Once you send a message, the recipient will receive the message through Mesibo_onMessage
. mesibo will inform the sender of the message about the status of the message - sent, delivered, or read through the listener Mesibo_onMessageStatus
as explained later in this part.
Add Media and Files to your message
You can also add a file using the file path, URL, or Image Object (Android Bitmap, iOS UIImage, etc) as required. For example,
msg.title = "This is a rich message";
msg.setContent(image);
msg.send();
Geotag your Messages
You can also geotag your messages by adding location information,
msg.latitude = 37.4275;
msg.longitude = 122.1697;
msg.send();
There is much more you can do, such as replying to a message, forwarding message, recall and delete message, etc. Refer to the messaging API documentation for more details.
Receiving Messages, Message Status, and Updates
As explained earlier, app needs to implement the mesibo MessageListener
class to receive messages, the status of messages you sent, and any other message updates.
Mesibo_onMessage
will be invoked whenever a message is sent or received. You can perform various checks to find if the message is incoming or outgoing, real-time or read from the database, group message or one-to-one message, etc. For example,
Complete code on GitHub for Javaopen_in_new
public class MainActivity extends AppCompatActivity implements mesibo.MessageListener,
Mesibo.ConnectionListener {
@Override
public void Mesibo_onConnectionStatus(int status) {
// You will receive the connection status here
Log.d(TAG, "on Mesibo Connection: " + status);
}
@Override
public void Mesibo_onMessage(MesiboMessage msg) {
// You will receive messages here
/* Messaging documentation https://docs.mesibo.com/api/messaging/ */
if(msg.isIncoming()) {
/* Profile documentation https://docs.mesibo.com/api/users-and-profiles/ */
MesiboProfile sender = msg.profile;
// check if this message belongs to a group
/* Group Management - https://docs.mesibo.com/api/group-management/ */
if(msg.isGroupMessage()) {
MesiboProfile group = msg.groupProfile;
}
// check if this message is realtime or read from the database
if(msg.isRealtimeMessage()) {
toast("You have got a message from " + sender.getNameOrAddress("")
+ ": " + msg.message);
}
} else if(msg.isOutgoing()) {
/* messages you sent */
} else if(msg.isMissedCall()) {
}
return true;
}
@Override
public void Mesibo_onMessageUpdate(MesiboMessage msg) {
// You will receive message updates here
return true;
}
@Override
public void Mesibo_onMessageStatus(MesiboMessage msg) {
// You will receive status of sent messages here
}
}
Complete code on GitHub for Kotlinopen_in_new,
override fun Mesibo_onConnectionStatus(status: Int) {
// You will receive the connection status here
}
override fun Mesibo_onMessage(message: MesiboMessage) {
// You will receive messages here
// A simple example of message processing
/* Messaging documentation https://docs.mesibo.com/api/messaging/ */
if (msg.isIncoming()) {
/* Profile documentation https://docs.mesibo.com/api/users-and-profiles/ */
val sender: MesiboProfile = msg.profile;
// check if this message belongs to a group
/* Group Management - https://docs.mesibo.com/api/group-management/ */
if(msg.isGroupMessage()) {
val group: MesiboProfile = msg.groupProfile;
}
// check if this message is realtime or read from the database
if(msg.isRealtimeMessage()) {
toast(
"You have got a message from " + sender.getNameOrAddress("")
.toString() + ": " + msg.message
)
}
} else if (msg.isOutgoing()) {
/* messages you sent */
} else if (msg.isMissedCall()) {
}
}
override fun Mesibo_onMessageUpdate(message: MesiboMessage) {
// You will receive message updates here
}
override fun Mesibo_onMessageStatus(message: MesiboMessage) {
// You will receive the status of sent messages here
}
Complete code on GitHub for Swiftopen_in_new
public func Mesibo_onConnectionStatus(status: Int) {
// You will receive the connection status here
}
func Mesibo_onMessage(message: MesiboMessage) {
// You will receive messages here
// A simple example of message processing
/* Messaging documentation https://docs.mesibo.com/api/messaging/ */
if(message.isIncoming()) {
/* Profile documentation https://docs.mesibo.com/api/users-and-profiles/ */
var sender: MesiboProfile = message.profile!;
/* Group Management - https://docs.mesibo.com/api/group-management/ */
// check if this message belongs to a group
if(message.isGroupMessage()) {
var group: MesiboProfile = message.groupProfile!;
}
// check if this message is realtime or read from the database
if (message.isRealtimeMessage()) {
alert("New Message", message: message.message)
}
}
}
func Mesibo_onMessageUpdate(message: MesiboMessage) {
// You will receive message updates here
}
func Mesibo_onMessageStatus(message: MesiboMessage)
// You will receive the status of sent messages here
}
Complete code on GitHub for Objective-Copen_in_new
-(void) Mesibo_onConnectionStatus:(NSInteger)status {
// You will receive the connection status here
NSLog(@"Connection status: %d", status);
}
-(void) Mesibo_onMessage:(MesiboMessage *)msg {
// You will receive messages here
// A simple example of message processing
/* Messaging documentation https://docs.mesibo.com/api/messaging/ */
if([msg isIncoming]) {
/* Profile documentation https://docs.mesibo.com/api/users-and-profiles/ */
MesiboProfile *sender = msg.profile;
/* Group Management - https://docs.mesibo.com/api/group-management/ */
// check if this message belongs to a group
if([msg isGroupMessage]) {
// the group profile
MesiboProfile *group = msg.groupProfile;
}
// check if this message is realtime or read from the database
if([msg isRealtimeMessage]) {
[self alert:@"New Message" message:msg.message];
}
} else if([msg isOutgoing]) {
/* messages you sent */
} else if([msg isMissedCall]) {
}
}
-(void) Mesibo_onMessageUpdate:(MesiboMessage *)msg {
// You will receive message updates here
}
-(void) Mesibo_onMessageStatus:(MesiboMessage *)msg {
// You will receive the status of sent messages here
}
Complete code on GitHub for Flutteropen_in_new
class _HomeWidgetState extends State<HomeWidget> implements MesiboConnectionListener, MesiboMessageListener, MesiboGroupListener {
@override
void Mesibo_onConnectionStatus(int status) {
print('Mesibo_onConnectionStatus: ' + status.toString());
}
@override
void Mesibo_onMessage(MesiboMessage message) {
String groupName = "";
if(null != message.groupProfile)
groupName = message.groupProfile!.name!;
if(message.isIncoming()) {
}
print('Mesibo_onMessage: from: (' + message.profile!.name! + ")
group: (" + groupName + ") Message: " + message.message!);
}
@override
void Mesibo_onMessageStatus(MesiboMessage message) {
print('Mesibo_onMessageStatus: ' + message.status.toString());
}
@override
void Mesibo_onMessageUpdate(MesiboMessage message) {
print('Mesibo_onMessageUpdate: ' + message.message!);
}
Complete code on GitHub for JavsScriptopen_in_new
function MesiboListener() {
}
MesiboListener.prototype.Mesibo_onConnectionStatus = function(status, value) {
console.log("Mesibo_onConnectionStatus: " + status);
}
MesiboListener.prototype.Mesibo_onMessageStatus = function(m) {
console.log("Mesibo_onMessageStatus: from "
+ m.peer + " status: " + m.status);
}
MesiboListener.prototype.Mesibo_onMessage = function(m) {
/* Messaging documentation https://docs.mesibo.com/api/messaging/ */
if(msg.isIncoming()) {
/* Profile documentation https://docs.mesibo.com/api/users-and-profiles/ */
var sender = msg.profile;
// check if this message belongs to a group
/* Group Management - https://docs.mesibo.com/api/group-management/ */
if(msg.isGroupMessage()) {
var group = msg.groupProfile;
}
// check if this message is realtime or read from the database
if(msg.isRealtimeMessage()) {
console.log("Mesibo_onMessage: from " + sender.getNameOrAddress("") + " msg: " + msg.message);
}
} else if(msg.isOutgoing()) {
/* messages you sent */
console.log("Mesibo_onMessage: sent a message with id: " + msg.mid);
} else if(msg.isMissedCall()) {
}
}
Complete code on GitHub for Pythonopen_in_new
class MesiboListener:
def __init__(self):
pass
def Mesibo_onConnectionStatus(self, status):
"""
Invoked when the connection status is changed.
It is also invoked when the token is about to be expired.
Different status codes convey the state of the connection.
Parameters
----------
status : Connection Status
"""
return 0
def Mesibo_onMessage(self, message):
"""
Invoked on receiving a new message or reading database messages
Parameters
----------
message: Message Object
"""
return 0
def Mesibo_onMessageUpdate(self, message):
"""
Invoked on receiving an update for existing message
Parameters
----------
message: Message Object
"""
return 0
def Mesibo_onMessageStatus(self, message):
"""
Invoked when the status of outgoing or sent message is changed
Parameters
----------
message: Message Object
"""
return 0
def Mesibo_onPresence(self, msg):
return 0
def Mesibo_onSync(self, count):
return 0
Complete code on GitHub for C++open_in_new
class SampleListener: public MesiboListener {
Mesibo *m_api;
public:
int Mesibo_onMessage(MesiboMessage *msg) {
logMessage(msg, "Mesibo_onMessage");
return 1;
}
int Mesibo_onMessageUpdate(MesiboMessage *msg) {
logMessage(msg, "Mesibo_onMessageUpdate");
return 1;
}
int Mesibo_onMessageStatus(MesiboMessage *msg) {
logMessage(msg, "Mesibo_onMessageStatus");
MesiboDateTime *dt = msg->getDeliveryTimestamp(NULL);
MesiboDateTime *rt = msg->getReadTimestamp(NULL);
if(dt) ERRORLOG("===> Delivery (date: %s) (time: %s)\n", dt->getDate(1), dt->getTime(1));
if(rt) ERRORLOG("===> Read (date: %s) (time: %s)\n", rt->getDate(1), rt->getTime(1));
return 1;
}
int Mesibo_onConnectionStatus(int status) {
ERRORLOG("===> Mesibo_onConnectionStatus: %u\n", status);
return 0;
}
int Mesibo_onPresence(MesiboPresence *p) {
return 0;
}
void Mesibo_onEndToEndEncryption(const char *address, int status) {
}
};
mesibo saves all the messages into a database so that you can retrieve them later. In the next section, we will learn how to read messages from the database.