JavaScript Video Tutorial
In 10 minutes we’ll build
A simple web app in vanilla javascript to make a video with audio from a caller to a callee.Get an API Key
You need a Telnx account to create an API key so you can interact with our Rooms API
- If you don’t have one please: Sign up for a free account
- Navigate to API Keys section and create an API Key by clicking
Create API Keybutton. - Copy your API key
Create a Room
Let’s use our Rooms API to create a room. RequestDon’t forget to update
YOUR_API_KEY here.id field, this is the id for your newly created Room.
Generate a client token
You’ll need a client access token to join the room. To create one use our REST APIrooms/create endpoint.
You’ll need to replace ROOM_ID in in the url of the command with your room id, above. As well as YOUR_API_KEY as you have in previous steps.
token field in the response is the client access token you’ll use to join the room.
🏁 Checkpoint - run the app
At this point you should have the following:- An API Key
- A room ID
- A room client access token
Let’s write some code
Basic concepts of the video SDK Here are some basic concepts of the SDK that will help you better understand how it works.-
A
Roomrepresents a real time audio/video/screen share session with other people or participants. It is fundamental to building a video application. -
A
Participantrepresents a person inside aRoom. EachRoomhas oneLocal Participantand one or moreRemote Participants. -
A
Streamrepresents the audio/video media streams that are shared byParticipantsin aRoom- A
Streamis indentified by it’sparticipantIdandstreamKey
- A
-
A
Participantcan have one or moreStream’s associated with it. -
A
Subscriptionis used to subscribe to aStreambelonging to aRemote Participant
Room instance that we’ll need to finish building this app that makes a video call.
connected- triggers when a room instance has connected to the serverparticipant_joined- triggers when a remote participant joins the roomstream_published- triggers when a stream has started being published to the roomsubscription_started- triggers when subscription to remote stream has started
🏁 Checkpoint
It might be helpful to take a look at the full list ofEventsavailable on aRoominstance.
Connect to the room and get local media
NOTE: We can start implementing our code after the room.initialize block inside the sandbox above.
Let’s connect to the room and use the standard WebRTC API to get the audio and video tracks from the device.
Publish the stream and render it
We want to a add/publish a stream the room which consists of the audio and video track we received from the user’s device, above. We’ll give the stream a key of “caller”, which is how the stream is identified.room.connected block looks like this:
Subscribe to the callee’s remote stream
As a caller, how do we know when the callee has joined the room or has started publishing a stream? That’s where theparticipant_joined and the stream_published events that we went over earlier, come into play.
We need to listen for teh stream_published event like this:
stream_published event is triggered for both local and remote streams. __
In our case, we want to know when the callee’s remote stream starts publishing so we can subscribe to it. We already know that the caller has published a “caller” stream so we can ignore that event.
Subscribing to a stream
When a stream is published in a room it doesn’t mean it’s audio and video tracks are accessible, yet. In order to access a remote stream’s track we must explicitly subscribe to that stream and wait for the subscription_started event.
Render the callee’s remote stream
We’re almost there! Now, we can listen to thesubscription_started event and use args from that handler to get the callee’s remote stream and render it to the DOM.
Run the app
To run the app:
- Open the sandbox, the browser here represents the caller’s app
- Open the app in another tab, this represents the callee’s application which you’ll need to interact with.
- Click the call button
- You should be prompted by the browser for camera/micrphone access please allow.
- You should also see video from your device’s camera and hear audio from the microphone
- You should see some console log, if things are working property
- If you see errors your client token has expired and you need to regenerate it.
- Click the call button
- Notice in the caller’s app a stream subscription was logged
- The callee’s video/audio stream is rendered.