Go#
This is a minimal server application built for Go with Gin that allows:
- Generating LiveKit tokens on demand for any application client.
- Receiving LiveKit webhook events.
It internally uses the LiveKit Go SDK.
Running this application#
Download the tutorial code:
To run this server application, you need Go installed on your device.
- Navigate into the server directory
- 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 Go app with a single file main.go
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 main.go
file:
main.go | |
---|---|
|
- The port where the application will be listening
- The API key of LiveKit Server
- The API secret of LiveKit Server
The main.go
file first 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.
Method getEnv
simply load each environment variable, giving a default value if not found.
The server launch takes place in the main
function at the end of the file, where we set the REST endpoints and start the server on SERVER_PORT
:
- Create a new Gin router
- Enable CORS support
- Create the
/token
endpoint - Create the
/webhook
endpoint - Start the server on the
SERVER_PORT
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.
main.go | |
---|---|
|
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - We set the video grants and identity of the participant 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.
We first load the request body into a struct with roomName
and participantName
string fields. 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 Go SDK:
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - We set the video grants and identity of the participant 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 and return it to the client.
- 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.
main.go | |
---|---|
|
- Create a
SimpleKeyProvider
with theLIVEKIT_API_KEY
andLIVEKIT_API
. - Receive the webhook event providing the
http.Request
in the Gin context and theSimpleKeyProvider
we just created. This will validate and decode the incoming webhook event. - Consume the event as you whish.
- Create a
SimpleKeyProvider
with theLIVEKIT_API_KEY
andLIVEKIT_API
. - Receive the webhook event providing the
http.Request
in the Gin context and theSimpleKeyProvider
we just created. This will validate and decode the incoming webhook event. - Consume the event as you whish.