Write your First mesibo Enabled Application - iOS

Estimated reading time: 7 minutes

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

Prerequisites

Please go through the following prerequisites before you read further.

  • Read the section First App 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.
  4. Import mesibo in your project In Objective-C,
#import <Mesibo/Mesibo.h>

In Swift,

import Mesibo
  1. Create two users, each with the User Address 123 and 456 and App ID com.mesibo.firstapp. You can refer to the section First App to learn how to create users.
  2. 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"
];

Initializing Mesibo

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

  1. Set your user credentials for authentication: access token using setAccessToken
  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

You can download the source code for the First Mesibo iOS App from GitHub.

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 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.
  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 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 Mesibo_onMessage listener.

Sending 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];
    _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)
    mMessage.text = ""
}

That’s it, Two users can now send messages to each other using your app! Click on LAUNCH MESSAGE UI on the app, to try all the messaging features!

Download the First iOS App Source Code from GitHub

You can download the source code from GitHub. Once you have set up two users and modified the tokens in the source code, you can install the app on two devices and try it out.

On one device, you can log in as User-1. On another device, you can log in as User-2. These two users can now text each other, send files to each other, make audio and video calls, etc. We will learn more about sending files and making calls in the upcoming sections.

First Mesibo Application: Javascript >>

mesibo, android, ios