Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal

Back to Tutorials

Text Chat Tutorial (iOS)

Overview

The OpenTok signaling API lets clients send text messages to other clients connected to the OpenTok session. You can send a signal message to a specific client, or you can send a message to every client connected to the session.

Setting up your project

To follow this tutorial:

  1. Clone the OpenTok iOS sample apps repo on GitHub:

    git clone https://github.com/opentok/opentok-ios-sdk-samples.git
  2. In terminal, cd to the Signaling subdirectory of the project, and then run pod install.
  3. Open your project in Xcode by double-clicking the new Signaling.xcworkspace file in the Signaling directory.
  4. Open the Config.h file, and set the SAMPLE_SERVER_BASE_URL string to the base URL of the server that implements the learning-opentok-php project. This server provides OpenTok session ID and tokens to the iOS sample app. For more information, see Setting up your server.

To send text chat messages between two clients connected to the OpenTok session, you can run the app in the XCode Simulator and in an iOS device.

Exploring the code

The archiving sample builds upon the Basic-Video-Chat sample, and it adds the following functionality:

When the user enters text in the text chat input text field, the [self sendChatMessage:] method is called:

- (void) sendChatMessage
{
    OTError* error = nil;
    [_session signalWithType:@"chat"
                      string:_chatInputTextField.text
                  connection:nil error:&error];
    if (error) {
        NSLog(@"Signal error: %@", error);
    } else {
        NSLog(@"Signal sent: %@", _chatInputTextField.text);
    }
    _chatTextInputView.text = @"";
}

This method calls the [OTSession signalWithType:string:connection:] method. This method sends a message to clients connected to the OpenTok session. Each signal is defined by a type string identifying the type of message (in this case "chat") and a string containing the message.

When another client connected to the session sends a message, the implementation of the [OTSessionDelegate session:receivedSignalType:fromConnection:withString:] method is called:

- (void)session:(OTSession*)session receivedSignalType:(NSString*)type fromConnection:(OTConnection*)connection withString:(NSString*)string {
    NSLog(@"Received signal %@", string);
    Boolean fromSelf = NO;
    if ([connection.connectionId isEqualToString:session.connection.connectionId]) {
        fromSelf = YES;
    }
    [self logSignalString:string fromSelf:fromSelf];
}

This method checks to see if the signal was sent by the local iOS client or by another client connected to the session:

Boolean fromSelf = NO;
if ([connection.connectionId isEqualToString:session.connection.connectionId]) {
    fromSelf = YES;
}

The session argument represents your client's OTSession object. The OTSession object has a connection property with a connectionId property. The connection argument represents the connection of client sending the message. If these match, the signal was sent by the local iOS app.

The method calls the [self logSignalString:] method which displays the message string in the text view for chat messages received.

This app uses the OpenTok signaling API to implement text chat. However, you can use the signaling API to send messages to other clients (individually or collectively) connected to the session.

Congratulations! You've finished the Text Chat Tutorial for iOS.
You can continue to play with and adjust the code you've developed here, or check out the Next Steps below. For more information on signaling, see the OpenTok signaling developer guide.