PHP#
This is a minimal server application built for PHP that allows:
- Generating LiveKit tokens on demand for any application client.
- Receiving LiveKit webhook events.
It internally uses LiveKit PHP SDK.
Running this application#
Download the tutorial code:
To run this server application, you need PHP and Composer 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.
Warning
LiveKit PHP SDK requires library BCMath. This is available out-of-the-box in PHP for Windows, but a manual installation might be necessary in other OS. Run sudo apt install php-bcmath
or sudo yum install php-bcmath
Understanding the code#
The application is a simple PHP app with a single file index.php
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.php
file:
index.php | |
---|---|
|
- Import all necessary dependencies from the PHP LiveKit library.
- Configure HTTP headers for the web server: enable CORS support, allow the
Content-Type
andAuthorization
headers and set the response content type toapplication/json
. - The API key of LiveKit Server.
- The API secret of LiveKit Server.
The index.php
file imports the required dependencies, sets the HTTP headers for the web server and loads the necessary environment variables:
LIVEKIT_API_KEY
: the API key of LiveKit Server.LIVEKIT_API_SECRET
: the API secret of LiveKit Server.
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.php | |
---|---|
|
- Create an
AccessTokenOptions
object with the participant's identity. - Create a
VideoGrant
object setting the necessary video grants options.setRoomJoin
allows the user to join a room andsetRoomName
determines the specific room. Check out all Video Grants. - We create the
AccessToken
providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
, initialize it with the token options, set the video grants and generate the 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 PHP SDK:
- Create an
AccessTokenOptions
object with the participant's identity. - Create a
VideoGrant
object setting the necessary video grants options.setRoomJoin
allows the user to join a room andsetRoomName
determines the specific room. Check out all Video Grants. - We create the
AccessToken
providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
, initialize it with the token options, set the video grants and generate the 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.php | |
---|---|
|
- Create a new
WebhookReceiver
object providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. It will help validating and decoding incoming webhook events. - The
Authorization
header of the HTTP request. - The raw body of the HTTP request as a string.
- Obtain the
WebhookEvent
object using theWebhookReceiver#receive
method. It takes the raw body as a String and the Authorization header of the request. - Consume the event as you wish.
We first create a WebhookReceiver
object using the LIVEKIT_API_KEY
and LIVEKIT_API_SECRET
. Then we must retrieve the Authorization
header and the raw body of the HTTP request. We need both of them to validate and decode the incoming webhook event.
Finally, we obtain the WebhookEvent
object using the WebhookReceiver#receive
method. It takes the raw body as a String and the Authorization header of the request. We can consume the event as we wish (in this case, we just log it using the error output).