Write your First mesibo Enabled Application - iOS

Estimated reading time: 18 minutes

In this part, we will create a simple real-time app in iOS.

Please go through the following prerequisites before you read further.

Prerequisites

  • 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

First iOS App

Now let’s quickly start coding:

  1. Start Xcode.
  2. Create a new project.
  3. Add mesibo.framework to your Xcode project. Refer Install Instructions for more details. Import mesibo API
  4. Create two users, each with the User Address 123 and 456 and App ID com.mesibo.firstapp. You can refer to the section Create Users to learn how to create users.
  5. Modify the token values in the source code to the ones you have generated for each of your demo users.

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"
];

Initialize mesibo for each user, when they log in.

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

Sample mesibo initialization code, in Objective-C

[MesiboInstance addListener:self];
[MesiboInstance setSecureConnection:YES];
[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()!.setSecureConnection(true)
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:(int)status {
    // You will receive the connection status here
    NSLog(@"Connection status: %d", status);
}

-(void) Mesibo_OnMessage:(MesiboParams *)params data:(NSData *)data {
    // You will receive messages here
}

-(void) Mesibo_OnMessageStatus:(MesiboParams *)params {
    // You will receive the status of sent messages here 
}   

Sample mesibo listeners in Swift

public func mesibo_(onConnectionStatus status: Int32){
    // You will receive the connection status here
    print("Connection status: \(status)")
}

func mesibo_(onMessage params: MesiboParams?, data: Data?){
    // You will receive messages here
}


func mesibo_(onMessageStatus params: MesiboParams?){
    // 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.

Testing your first application

  1. Build and Install the Application.
  2. On the demo app, click on the Login as User1 button.
  3. Mesibo_onConnectionStatus should cycle 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. If you are not getting connection status 1, and getting 4 or 5, you are not passing the correct appid while creating a token. The appid passed to create a token must match the iOS bundle id declared in the Xcode project (for example, com.mesibo.xxx).

  4. We will use mesibo console to send a test message. In a later section, we will learn how to send messages from the app itself.
    • Go to Console ->Application->Users. You should see the user you have created.
    • Go to user details by clicking the Edit button. Scroll down, you will see a section to Message User
    • Enter 1000 (or anything) in the From field, check Create This User checkbox, type the message and click on Send.
  5. You will instantly receive this message in your mobile app and the message will be displayed in a toast.

If you check the logs, you can see that you got the message in the Mesibo_onMessage listener.

Sending and Receiving Messages

In the previous section, we have used mesibo console to send a message. Now we will quickly learn how to send messages from the app itself. To send messages, we will use sendMessage real-time API for which we will need destination user, message-id, and the message itself.

Invoke the following function anywhere from your code to send a text message. In a later section, we will learn how to send rich messages.

In Objective-C

- (IBAction)onSendMessage:(id)sender {
    MesiboParams *params = [MesiboParams new];
    params.peer = mRemoteUser;
    params.flag = MESIBO_FLAG_READRECEIPT | MESIBO_FLAG_DELIVERYRECEIPT;

    uint32_t mid = [MesiboInstance random];
    [MesiboInstance sendMessage:params msgid:mid string:_mMessage.text];
}

In Swift

@IBAction func onSendMessage(_ sender: Any) {
    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: mMessage.text)
}

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 bill any additional amount for the voice or video call API.

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.

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 address:mRemoteUser 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

You can invoke one of the messaging APIs to send a group message, just like the way you used the one-to-one messaging API. Here, instead of the user’s address you need to specify the groupid. To learn about creating a group and getting the groupid refer to Group Management APIs.

For example, here is an example of how you use sendMessage to send a message to a group. Let’s say you have created a group with the group-id 96568. Then, you use the same function that you use for sending a one-to-one message, where instead of setting the peer to the user address, now you set it to the group -id.

Using Objective-C,

MesiboParams *params = [MesiboParams new];
params.groupid = 96568;
params.flag = MESIBO_FLAG_READRECEIPT | MESIBO_FLAG_DELIVERYRECEIPT;
uint32_t mid = [MesiboInstance random];
NSString *message = @"This is a group message";
[MesiboInstance sendMessage:params msgid:id string:message];

Using Swift,

let params = MesiboParams()
params.peer = mRemoteUser
params.flag = (UInt32)(MESIBO_FLAG_READRECEIPT | MESIBO_FLAG_DELIVERYRECEIPT)
let mid = Mesibo.getInstance()!.random()
let message = "This is a group message"
Mesibo.getInstance()!.sendMessage(params, msgid: mid, string: message)

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.

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

mesibo provides a set of APIs deal with stored messages 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 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 for a user or a group, In Objective-C,

MesiboReadSession *mReadSession = [MesiboReadSession new];
[mReadSession initSession:mRemoteUser groupid:0 query:nil delegate: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 initSession:nil groupid:0 query:nil delegate:self];
[mReadSession enableSummary:YES];
[mReadSession read:100];

To read messages for a user or a group, In Swift,

mReadSession = MesiboReadSession()
mReadSession.initSession(mRemoteUser, groupid: 0, query: nil, delegate: 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 <img src=”images/ios-first-app-message.png” width=”300” height=”500” hspace=10/>

To launch the messaging UI for a user, you need to pass the user’s address.

Here, is an example in Objective-C

- (IBAction)onLaunchMessagingUIModule:(id)sender {
    //requires pod mesibo-ui
    [MesiboUI launchMessageViewController:self profile:mProfile];
}

Here, is an example in Swift

@IBAction func onLaunchMessagingUIModule(_ sender: Any) {
     //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: Javascript >>

mesibo, ios