Sending a Signal
Now that you have connected to the session and have a session object, you can now send a Signal. Signals are arbitrary text or data that are sent in a session which can be sent to a specific client or all connected clients. You can learn more about them in the Vonage Video Signaling developer guide. For this tutorial you will be sending text.
- Copy the following code and paste it below the existing code in your
VonageVideoSDK.swiftfile after theinitfunction:
func sendSignal(type: String, data: String) {
var error: OTError?
session.signal(withType: type , string: data, connection: nil, error: &error)
if let error = error {
print("Session creation error \(error.description)")
} else {
messages.append(.init(text: data, isUsers: true))
}
}
This code adds a function that will be called from the UI. session.signal is called to send the text as a Signal. session.signal takes the type of Signal and some data. The type is optional but can be used to differentiate between types of Signals in your app. If the Signal sends successfully, it is added to the array of messages that the UI is using. Now update the UI:
- Open
ContentView.swift - Copy the following over the existing view code:
struct ContentView: View {
@StateObject private var sdk = VonageVideoSDK()
@State private var message: String = ""
var body: some View {
VStack {
if (sdk.isSessionConnected) {
List(sdk.messages) { signal in
Text(signal.text)
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity, alignment: signal.isUsers ? .trailing : .leading)
}
HStack {
TextField("Message", text: $message)
Button("Send") {
sdk.sendSignal(type: "msg", data: message)
self.message = ""
}.buttonStyle(.bordered)
}.padding(8)
} else {
ProgressView {
Text("Connecting ...")
.font(.title)
}
}
}
}
}
This code first defines variables for an object of the VonageVideoSDK from earlier and for the message that will be entered. The UI consists of a List which will display the array of from the VonageVideoSDK class, a TextField to enter a message and a Button to send the message. When the button is pressed, it calls the sendSignal function you just created.
Basic text chat
Follow this tutorial to build basic text chat from scratch using the Vonage Video API. It is the quickest way to build a proof of concept for this functionality on the video platform.