How to add text chat to opentok video chat

276 Views Asked by At

I am working on an IOS application(SWIFT) in which i have used tokbox sdk for adding video chat into ios app. now i want to add text chat option during video call!

Can anyone guide how to add text chat option during video call in iOS app using tokbox sdk?

1

There are 1 best solutions below

7
Raja Kishan On

You can use signal for chat and sending data between clients connected to an OpenTok session.

Here is the reference like : https://tokbox.com/developer/guides/signaling/ios-swift/

In doc have full information. Here is the sample code.

import OpenTok

enum TokBoxSignalType: String {
    case message = "message"
}

class ViewController: UIViewController {
    
    private var openTokSession: OTSession?
    
    @IBAction func onSendMessage(_ sender: UIButton) {
        //send message to OTSignal
        var error: OTError?
        defer {
            self.handleOTError(error)
        }
        openTokSession?.signal(withType: TokBoxSignalType.message.rawValue, string: "send_chat_message", connection: nil, error: &error)
    }
    
    
    //Handle OpenTok errors
    private func handleOTError(_ error: OTError?) {
        if let error = error {
            print("There is an error : ", error.localizedDescription)
        }
    }
    
}


//MARK : Signal Methods
extension ViewController {
    func session(_ session: OTSession, receivedSignalType type: String?, from connection: OTConnection?, with string: String?) {
        guard let typeEnum = TokBoxSignalType(rawValue: type ?? "") else {
            return
        }
        
        switch typeEnum {
        case .message:
            print(string)
        }
    }
}