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";
initializeSession();
// Handling all of our errors here by alerting them
function handleError(error) {
if (error) {
alert(error.message);
}
}
function initializeSession() {
const session = OT.initSession(applicationId, sessionId);
// Subscribe to a newly created stream
session.on('streamCreated', function(event) {
session.subscribe(event.stream, 'subscriber', {
insertMode: 'append',
width: '100%',
height: '100%'
}, handleError);
});
// Create a publisher
const publisher = OT.initPublisher('publisher', {
insertMode: 'append',
width: '100%',
height: '100%'
}, handleError);
// Connect to the session
session.connect(token, function(error) {
// If the connection is successful, publish to the session
if (error) {
handleError(error);
} else {
session.publish(publisher, handleError);
}
});
}
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.
- Open the
index.htmlfile in your browser by double-clicking on it. - The browser will ask for permission to use your camera and microphone, go ahead and allow it access.
- You should see yourself appear on the screen.
Next, we can test what it looks like if someone else connects. While our code is not reachable from the internet, we can simulate that by opening this page in another window.
- Mute yourself to avoid any feedback.
- Copy the URL in the address bar of your browser.
- Open a new window or tab, and paste in the address to open the file.
- You should see yourself twice now (with one mirrored from the other, depending on your browser).
Troubleshooting tip: If there's no video showing up on the page, 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 video chat
Learn the basic concepts of the Vonage Video API platform, including how users can communicate through video, voice, and messaging. Explore a basic Vonage Video API flow.