JavaScript

Testing your Code in a Browser

At this point, your app.js file should look something like this (with a few adjustments):

// replace these values with those generated in your Video API account
var applicationId = "YOUR_APP_ID";
var sessionId = "YOUR_SESSION_ID";
var token = "YOUR_TOKEN";

var session;

initializeSession();

// Handling all of our errors here by alerting them
function handleError(error) {
  if (error) {
    alert(error.message);
  }
}

function initializeSession() {
  session = OT.initSession(applicationId, sessionId);

  // Connect to the session
  session.connect(token, function(error) {
    // If the connection is successful, publish to the session
    if (error) {
      handleError(error);
    }
  });

  // Receive a message and append it to the history
  const msgHistory = document.querySelector('#history');
  session.on('signal:msg', (event) => {
    const msg = document.createElement('p');
    msg.textContent = event.data;
    msg.className = event.from.connectionId === session.connection.connectionId ? 'mine' : 'theirs';
    msgHistory.appendChild(msg);
    msg.scrollIntoView();
  });
}

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 = '';
    }
  });
});

In your completed code, you should have hard coded values to replace YOUR_APP_ID, YOUR_SESSION_ID and YOUR_TOKEN — if you haven't done this, see Setting up authentication above.

  1. Open the index.html file in your browser by double-clicking on it.
  2. Enter some text into the form and send it.
  3. You should see the message get added to the message history.

Next, we can test what it looks like if someone else sends a message. While our code is not reachable from the internet, we can simulate that by opening this page in another window.

  1. Copy the URL in the address bar of your browser.
  2. Open a new window or tab, and paste in the address to open the file.
  3. Enter some text into the form and send it.
  4. You should see a second message get added to the message history.

Troubleshooting tip: If you cant send and receive messages, open the "console" tab in your browser tools (command+option+i on Mac, CTRL+i on Windows) and check for errors. The most likely issue is that your API key, session ID, or token is not set up properly. Since you hard coded your credentials, it's also possible that your token has expired.

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.

Steps
1
Overview
2
Before You Begin
3
Configure a Vonage Video Application
4
Creating the Project Folders and HTML Template
5
Setting Up Authentication
6
Connecting to the Session
7
Sending a Signal
8
Receiving a Signal
9
Testing your Code in a Browser
10
A little bit of CSS customization
11
Conclusion