Node#
This is a minimal server application built for Node with Express that allows:
- Generating LiveKit tokens on demand for any application client.
- Receiving LiveKit webhook events.
It internally uses LiveKit JS SDK.
Running this application#
Download the tutorial code:
To run this server application, you need Node installed on your device.
- Navigate into the server directory
- Install dependencies
- Run the application
Info
You can run any Application Client to test against this server right away.
Understanding the code#
The application is a simple Express app with a single file index.js
that exports two endpoints:
/token
: generate a token for a given Room name and Participant name./webhook
: receive LiveKit webhook events.
Let's see the code of the index.js
file:
index.js | |
---|---|
|
- Import
AccessToken
fromlivekit-server-sdk
. - The port where the application will be listening.
- The API key of LiveKit Server.
- The API secret of LiveKit Server.
- Initialize the Express application.
- Enable CORS support.
- Enable JSON body parsing for the
/token
endpoint. - Enable raw body parsing for the
/webhook
endpoint.
The index.js
file imports the required dependencies and loads the necessary environment variables:
SERVER_PORT
: the port where the application will be listening.LIVEKIT_API_KEY
: the API key of LiveKit Server.LIVEKIT_API_SECRET
: the API secret of LiveKit Server.
It also initializes the WebhookReceiver
object that will help validating and decoding incoming webhook events.
Finally the express
application is initialized. CORS is allowed, JSON body parsing is enabled for the /token
endpoint and raw body parsing is enabled for the /webhook
endpoint.
Create token#
The endpoint /token
accepts POST
requests with a payload of type application/json
, containing the following fields:
roomName
: the name of the Room where the user wants to connect.participantName
: the name of the participant that wants to connect to the Room.
index.js | |
---|---|
|
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
,LIVEKIT_API_SECRET
and setting the participant's identity. - We set the video grants in the AccessToken.
roomJoin
allows the user to join a room androom
determines the specific room. Check out all Video Grants. - We convert the AccessToken to a JWT token.
- Finally, the token is sent back to the client.
The endpoint first obtains the roomName
and participantName
parameters from the request body. If they are not available, it returns a 400
error.
If required fields are available, a new JWT token is created. For that we use the LiveKit JS SDK:
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
,LIVEKIT_API_SECRET
and setting the participant's identity. - We set the video grants in the AccessToken.
roomJoin
allows the user to join a room androom
determines the specific room. Check out all Video Grants. - We convert the AccessToken to a JWT token.
- Finally, the token is sent back to the client.
Receive webhook#
The endpoint /webhook
accepts POST
requests with a payload of type application/webhook+json
. This is the endpoint where LiveKit Server will send webhook events.
index.js | |
---|---|
|
- Initialize the WebhookReceiver using the
LIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. It will help validating and decoding incoming webhook events. - The body of the HTTP request.
- The
Authorization
header of the HTTP request. - Consume the event as you whish.
First of all we initialize the WebhookReceiver
using the LIVEKIT_API_KEY
and LIVEKIT_API_SECRET
. This object will validate and decode the incoming webhook events.
The endpoint receives the incoming webhook with the async method WebhookReceiver#receive
. It takes the body and the Authorization
header of the request. If everything is correct, you can do whatever you want with the event (in this case, we just log it).
Remember to return a 200
OK response at the end to let LiveKit Server know that the webhook was received correctly.