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
app.jsfile after theinitializeSession()function:
const form = document.querySelector('form');
const msgTxt = document.querySelector('#msgTxt');
// Send a signal once the user enters data in the form
form.addEventListener('submit', (event) => {
event.preventDefault();
session.signal({
type: 'msg',
data: msgTxt.value
}, (error) => {
if (error) {
handleError(error);
} else {
msgTxt.value = '';
}
});
});
This code creates variables to reference the form and it's containing text from the earlier html page. Once the form is submitted, 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 the form is cleared, if not the error is handled using the same handleError function from earlier.
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.