Java#
This is a minimal server application built for Java with Spring Boot that allows:
- Generating LiveKit tokens on demand for any application client.
- Receiving LiveKit webhook events.
It internally uses LiveKit Kotlin SDK.
Running this application#
Download the tutorial code:
To run this server application, you need Java and Maven 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 Spring Boot app with a single controller Controller.java
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 Controller.java
file:
Starting by the top, the Controller
class has the following annotations:
@CrossOrigin(origins = "*")
: allows the application to be accessed from any domain.@RestController
: marks the class as a controller where every method returns a domain object instead of a view.
Going deeper, the Controller
class has the following fields:
LIVEKIT_API_KEY
: the API key of LiveKit Server. It is injected from the propertylivekit.api.key
defined inapplication.properties
using the@Value("${livekit.api.key}")
annotation.LIVEKIT_API_SECRET
: the API secret of LiveKit Server. It is injected from the the propertylivekit.api.secret
defined inapplication.properties
using the@Value("${livekit.api.secret}")
annotation.
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.
Controller.java | |
---|---|
|
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 Kotlin SDK:
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - We set participant's name and identity in the AccessToken.
- We set the video grants in the AccessToken.
RoomJoin
allows the user to join a room andRoomName
determines the specific room. Check out all Video Grants. - 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.
Controller.java | |
---|---|
|
We declare the 'Authorization' header and the raw body of the HTTP request as parameters of the our method. We need both of them to validate and decode the incoming webhook event.
Then we initialize a WebhookReceiver
object using the LIVEKIT_API_KEY
and LIVEKIT_API_SECRET
.
Finally we obtain a WebhookEvent
object calling method WebhookReceiver#receive
. It takes the raw body as a String 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.