Write your First mesibo Enabled Application - iOS

Estimated reading time: 23 minutes

In this part, we will create a simple real-time messaging and calling app for iOS. This app will let you log in as two different users so that you can try various mesibo features.

We will quickly explore the following

  • Sending and Receiving Messages

  • Voice and Video Calls

  • Group Messaging

  • Group Calls (Conferencing)

  • Reading Messages from database

  • Using UI Modules

Prerequisites and Download Source Code

  • Read the section Create Users to learn about creating an app in mesibo console and creating users & access tokens for your app.
  • Knowledge of using Xcode and making basic iOS Apps
  • Download the source code for the First mesibo iOS App from GitHub
  • [Optional] You can download and run mesibo messenger app on one of your phones so that you can use it to test the First App.

Configure and Build First iOS App

In this step, we will configure and build the First App. Once you run it, we will get into more details.

Step 1 Open Project

Start Xcode and open the project that you downloaded from GitHub.

This project already has all the mesibo Frameowrks configured. However, you should update frameworks by running command

$ pod update

Also, in the future, if you are creating a new project, you can add mesibo frameworks to your project as explained in installation instructions.

Step 2 Configure Mesibo Tokens

Before we can build and run the app, we need to configure app with valid mesibo user tokens as explained in Create Users section.

Create tokens for two users, each with the User Address 123 and 456 and App ID com.mesibo.firstapp and configure two demo users in the source code with the tokens you have generated.

Example, in Objective-C

mUser1 = @{
    @"token": @"xyz",\
    @"name": @"User 1",\
    @"address": @"123"};
    
mUser2 = @{
    @"token": @"pqr",\
    @"name": @"User 2",\
    @"address": @"456"};

Example, in Swift

var mUser1: [String: String] = [
    "name": "User 1",
    "address": "123",
    "token": "xyz"
];
    
var mUser2: [String: String] = [
    "name": "User 2",
    "address": "456",
    "token": "pqr"
];

Step 3 Build and Run

Build and Run the project. It may take a few seconds and then you should see the following screen

You should install the app on two devices (or simulators). On the first device, click on the Login as User1 button and on another device, click on the Login as User2 button. Note that call feature requires real devices (not the simulator).

If you add a breakpoint on Mesibo_onConnectionStatus, you will see that it cycles through various status information. Finally, you should receive status=1 which indicates that your app is successfully connected to the mesibo real-time server and ready to send and receive real-time messages.

You can now send messages to each other and can also make voice and video calls.

If you check the logs or add a breakpoint on Mesibo_onMessage, you can see that you got the message in Mesibo_onMessage listener.

Once you have tried all the demo features, you can start going into details below.

Code Explanation

Initialize mesibo

Before you send and receive real-time messages and calls, you need to initialize mesibo. The initialization involves the following steps:

  1. Use setAccessToken to set the user’s access token that you obtained while creating the user
  2. Set up mesibo to invoke your listener class using addListener
  3. Set up database to store sent and received messages using setDatabase (Optional)
  4. Start mesibo

Note that, except addListener, all other initialization functions should be only called once.

Sample mesibo initialization code, in Objective-C

[MesiboInstance addListener:self];
[MesiboInstance setAccessToken:token];
[MesiboInstance setDatabase:@"mydb" resetTables:0];
    
[MesiboInstance start];

//... Refer to the Sample Code on GitHub...//

}

Sample mesibo initalization code, in Swift

Mesibo.getInstance().addListener(self)
Mesibo.getInstance().setAccessToken(token)
Mesibo.getInstance().setDatabase("mydb", resetTables: 0)

Mesibo.getInstance().start()
    
//... Refer to the Sample Code on GitHub...//

Sample mesibo listeners in Objective-C

-(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://mesibo.com/documentation/api/messaging/ */
    if([msg isIncoming]) {
        /* Profile documentation https://mesibo.com/documentation/api/users-and-profiles/ */
        MesiboProfile *sender = msg.profile;

        /* Group Management - https://mesibo.com/documentation/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 
}   

Sample mesibo listeners in Swift

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://mesibo.com/documentation/api/messaging/ */
        if(message.isIncoming()) {
            
            /* Profile documentation https://mesibo.com/documentation/api/users-and-profiles/ */
            var sender: MesiboProfile = message.profile!;
            
            /* Group Management - https://mesibo.com/documentation/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 
}   

That’s it - you are now ready to send and receive your first real-time message.

Select Destination

As explained earlier, mesibo has two types of destinations, individual users and groups. Every valid mesibo user and group in your app are represented by MesiboProfile object. The MesiboProfile object has all the information about the user or group, for example, name, status, photo, etc. You can get a profile for any user or group by calling getProfile API. For example,

In Objective-C,

mProfile = [MesiboInstance getProfile:address groupid:0];

In Swift,

mProfile = Mesibo.getInstance().getProfile(address, groupid: 0)

Once you have the profile, you can perform various APIs like accessing profile information, sending messages to the destination, etc.

Sending and Receiving Messages

Now we will quickly learn how to send messages in real-time.

To send messages, you only need to create a MesiboMessage object for the destination, add your content (text message, files, location, etc.) and call send method. You can create a MesiboMessage object by calling

In Swift,

let message = profile.newMessage();

OR, In Objective-C,

MesiboMessage *message = [profile newMessage];

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.

In Objective-C

MesiboMessage *msg = [profile newMessage];
msg.message = @"Hello";
msg.title = "My Message Title";
[msg send];

In Swift

let msg = profile.newMessage()
msg.message = "Hello";
msg.title = "My Message Title";
msg.send();

You can also add a file using file path, URL, or UIImage as required. For example,

msg.setContent(image);
msg.send();

You can also geotag your messages by adding location information,

msg.latitude = 37.4275;
msg.longitude = 122.1697;
msg.send();

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.

Voice and Video Calls

mesibo allows you to enable peer-to-peer HD video and voice calls between your users in just a few lines of code. As with messaging, mesibo video and voice calls are encrypted and hence secure. mesibo does not charge any additional amount for the voice or video call APIs.

Making and receiving voice & video calls with mesibo is simple. You only need to initialize MesiboCall once using just two lines of code! Then, mesibo will automatically handle incoming calls. For outgoing calls, you can invoke call API as required.

You can use mesibo voice and video calls as it is. But, if you wish, you can customize it to your needs. For example, you can customize the call screen UI, filter incoming calls, etc. Refer to Call API Reference to learn more.

Video and Voice Call APIs

Initialization in Objective-C

[MesiboCall startWith:nil name:@"mesibo first App" icon:nil callKit:YES];

To make a call in Objective-C

 [MesiboCallInstance callUi:self profile:mProfile video:YES];

In the first app tutorial we have now learned about two of the most fundamental mesibo APIs - the messaging API and call API. In the upcoming sections, we will learn more about various mesibo APIs and features that you can use. Such as group messaging, group calls, reading stored messages, UI Modules, etc.

Group Messaging and Group Calls

In this section, we will learn about group messaging and group calls(conferencing). The group messaging API is similar to the one-to-one messaging API and the group call API is similar to the one-to-one voice and video call API.

Group Messaging

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.

For example, here is an example of how you can send a message to a group. Let’s say you have a group with the group-id 96568. Now, you use the same function that you use for sending a one-to-one message, but instead of using the user profile, you first get the group profile using the group-id and use it to send a group message.

Using Objective-C,

profile = [MesiboInstance getProfile:nil groupid:96568];
MesiboMessage *msg = [profile newMessage];
msg.message = @"This is a group message";
[msg send];

Using Swift,

profile = Mesibo.getInstance().getProfile(nil, groupid: 96568)
let msg = profile.newMessage()
msg.message = "This is a group message"
msg.send()

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.

Creating a Group

To create a group and add members, you need to use the Group Management APIs. This is the recommended approach. However, you can also create a group and add/remove members by using backend APIs and mesibo console if requires.

You can create a new group by calling createGroup API, with name and the Group Listener Object.

In Java,

MesiboInstance createGroup:@"My Group" flags:0 listener:self];

It will send a request to the server to create a new group. When a group is created, callback function in the listener will be called with the group profile. You can then add members to the group using group profile APIs.

-(void) Mesibo_onGroupCreated:(MesiboProfile *) profile {
        NSLog(@"New group");
}

You can now use this new group profile for messaging, adding more members or changing group name, description, picture, etc.

Adding members to a Group

You can add members, and admins to the group by calling addMembers API of the group profile. You need to pass the addresses of the users to be added as group members, permissions to be granted and admin permissions if you are adding them as group admins.

For example,

NSArray *members = @[@"18005550001", @"18005550002", @"18005550003"];
MesiboGroupProfile *gp = [mProfile getGroupProfile];
[gp addMembers:members permissions:MESIBO_MEMBERFLAG_ALL adminPermissions:0];

Group Calls (Conferencing)

mesibo group calling simply extends the existing features of group messaging with additional permissions and control, for example, room resolution, talk detection, who can publish and who can video, voice+video or voice only, etc. Your existing groups will now have additional conferencing settings.

To add mesibo Group Calling to your app, here is an overview of steps to follow:

  1. Have a group of members with required permissions
  2. Create a group call object with groupCall()
  3. To publish your stream to the group, get a Participant object using createPublisher and make a call()
  4. To subscribe to other users’ streams, get their call object in MesiboGroupCall_OnPublisher and make a call()
  5. Display the stream using setVideoView

For detailed documentation and examples for conferencing refer to Mesibo Conferencing Docs and download the example source code for conferencing from GitHub

You can also try a live demo of a Zoom-like conferencing app, Mesibo Live built using mesibo conferencing and streaming API. The source code of the app is available on Github. You can download it, modify or rebrand it, and integrate it into your app or website.

Reading Messages & Call History

mesibo provides a set of APIs to read messages and call history stored in the database and sending read receipts. A read receipt is an acknowledgment that the user has read the message.

To read stored messages from the database, you need to create a read session and set the criteria to read messages; for example,

  • read all messages
  • read call history
  • read messages from a sender
  • read messages for a particular group
  • read messages matching a search query etc.

The read session you create filters the messages in the database based on the criteria provided and gives the resulting list of messages to you.

Reading Modes

There are two modes of operation:

  • Read Messages and call logs. This mode is enabled by default
  • Read Summary (active conversations), read the latest message from each user and group. This allows you to get a snapshot of all the communicating users and groups. You can then create another read session to read messages for any of those users or groups.

Reading Order

Once you set a read session, you can start reading messages by calling read API. You can read messages in the first-in-first-out (FIFO) mode or the last-in-first-out (LIFO) mode. In the first-in-first-out model, the oldest messages are read first. By default, the first-in-first-out mode is disabled.

Read Receipts

You can enable the automatic sending of read-receipts every time you read a message. This can be achieved by enabling a read-receipt for the read session. On reading or receiving a message, read receipts will be automatically sent if

  • The app is set to be in the foreground
  • Sending Read Receipt is enabled for the reading session, AND
  • Read receipt requested by the sender, AND
  • A new real-time or database message matches the read session.

A call to read will return the number of messages read. You can call read on demand. For example, in the case of a messaging app, you only need to show the latest messages on the screen initially. So, first, you can call read(10) to load the last 10 messages. As the user scrolls up the chat history, you can call read again. All subsequent calls to read will read older messages.

To read messages and call history for a user or a group, In Objective-C,

MesiboReadSession *mReadSession = [mProfile createReadSession:self];
[mReadSession enableReadReceipt:YES];
[mReadSession read:100];

To read the last message from each user and group (summary), In Objective-C,

MesiboReadSession *mReadSession = [MesiboReadSession new];
[mReadSession enableSummary:YES];
[mReadSession read:100];

To read messages and call history for a user or a group, In Swift,

mReadSession = mProfile.createReadSession(self)
mReadSession.enableReadReceipt(true)
mReadSession.read(100)

To read last message from each user and group (summary), In Swift,

mReadSession = MesiboReadSession()
mReadSession.initSession(nil, groupid: 0, query: nil, delegate: self)
mReadSession.enableSummary(true)
mReadSession.read(100)

Ready to use UI Modules

mesibo provides a variety of pre-built UI components that allow you to quickly add messaging, voice & video call graphical user interface in your app. mesibo UI modules are completely customizable. You can not only customize colors, icons, etc but also customize how each message is rendered. This enables you to create powerful chatbots in no-time.

mesibo UI modules are available as:

  • Fragment (Android)
  • ViewController (iOS)
  • Individual Views (android and ios)

One of the key advantages of mesibo UI modules is that they are well tested and you only need to write minimal code to create fully-functional UIs. Any future enhancements are also automatically available to you.

messaging group chat mesibo voice and video calls

To use mesibo UI modules, you need to perform the following steps:

  1. Add UI modules to your project
  2. Initialize mesibo API with access token as described in the Android, or iOS section
  3. Launch mesibo UI modules, for example,
MesiboUI.launch(context, 0, false, true);

That’s it.

The best way to learn how to use mesibo UI modules is to refer to the open-source messenger app for Android, iOS, and Javascript source code which uses mesibo UI modules for various functionalities like welcome screen, login, media picker, one-to-one and group messaging, user list, group management, showing profiles, etc.

In the following sections, we will explain how you can launch mesibo UI modules for messaging and calls. The code examples are taken from source code available on GitHub - First mesibo App for Android andFirst mesibo App for iOS

Launching Messaging UI in iOS

  1. Install mesibo UI Modules by following Instructions for iOS

  2. Import the mesibo UI Module.
    In Objective-C,
    #import "MesiboUi/MesiboUi.h"
    

    In Swift,

    import MesiboUI
    
  3. In the case of one-to-one messaging, you will need to launch the message UI for each user. For example, if there is a user named User2, you will need to launch the messaging UI like below

To launch the messaging UI for a user, you need to pass the profile.

Here, is an example in Objective-C

    //requires pod mesibo-ui
    [MesiboUI launchMessageViewController:self profile:mProfile];

Here, is an example in Swift

     //requires pod mesibo-ui
     MesiboUI.launchMessageViewController(self, profile: mProfile)

Push Notifications and Webhooks

Your app can use push notifications to alert a user about important events. For example, when the user is offline, and there is a new message, you can display a push notification so that the user can click on it and view the message. To send push notifications, you can use FCM( Firebase Cloud Messaging) or APN(Apple Push Notification). You can send all types of push notifications — background push notifications(silent notifications) and alerts through mesibo. All you need to do is configure your FCM/APN push credentials in the console. Then, mesibo will automatically send push notifications to your users on behalf of you.

You can learn more about using Push Notifications with mesibo here.

A WebHook is an HTTP(S) URL that will be invoked with POST data in real-time by mesibo when something happens; for example, an offline message, a user comes online or goes offline, etc. You can learn more about using webhooks with mesibo here.

First mesibo Application: Xaramin >>

mesibo, ios