agence104 / livekit-server-sdk
Server-side SDK for LiveKit.
Requires
- php: ^8.1
- firebase/php-jwt: ^7.0.0
- google/protobuf: ^3.23|^4.0
- guzzlehttp/guzzle: ^6.3|^7.0
- guzzlehttp/psr7: ^1.6.1|^2.0
- twirp/twirp: ^0.9.1|^0.14.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.7
README
Use this SDK to interact with livekit server APIs and create access tokens from your PHP Backend.
Installation
Requirements
- php: >= 8.1
Composer
composer require agence104/livekit-server-sdk
Usage
Environment Variables
You may store credentials in environment variables. If host, api-key or api-secret is not passed in when creating a RoomServiceClient or AccessToken, the values in the following env vars will be used:
- LIVEKIT_URL
- LIVEKIT_API_KEY
- LIVEKIT_API_SECRET
Creating Access Tokens
Creating a token for participant to join a room.
use Agence104\LiveKit\AccessToken; use Agence104\LiveKit\AccessTokenOptions; use Agence104\LiveKit\VideoGrant; // If this room doesn't exist, it'll be automatically created when the first // client joins. $roomName = 'name-of-room'; // The identifier to be used for participant. $participantName = 'user-name'; // Define the token options. $tokenOptions = (new AccessTokenOptions()) ->setIdentity($participantName); // Define the video grants. $videoGrant = (new VideoGrant()) ->setRoomJoin() ->setRoomName($roomName); // Initialize and fetch the JWT Token. $token = (new AccessToken('api-key', 'secret-key')) ->init($tokenOptions) ->setGrant($videoGrant) ->toJwt();
By default, the token expires after 6 hours. you may override this by passing in ttl in the access token options. ttl is expressed in seconds (as number) .
Parsing the Access Tokens
Converting the JWT Token into a ClaimGrants.
use Agence104\LiveKit\AccessToken; // Initialize and parse the JWT Token. $claimGrants = (new AccessToken('api-key', 'secret-key')) ->fromJwt($token);
Permissions in Access Tokens
It's possible to customize the permissions of each participant:
use Agence104\LiveKit\VideoGrant; $videoGrant = (new VideoGrant()) ->setRoomJoin() // TRUE by default. ->setRoomName('name-of-room') ->setCanPublish(FALSE) ->setCanSubscribe() // TRUE by default. ->setGrant($videoGrant);
This will allow the participant to subscribe to tracks, but not publish their own to the room.
Managing Rooms
RoomServiceClient gives you APIs to list, create, and delete rooms. It also requires a pair of api key/secret key to operate.
use Agence104\LiveKit\RoomServiceClient; use Agence104\LiveKit\RoomCreateOptions; $host = 'https://my.livekit.host'; $svc = new RoomServiceClient($host, 'api-key', 'secret-key'); // List rooms. $rooms = $svc->listRooms(); // Create a new room. $opts = (new RoomCreateOptions()) ->setName('myroom') ->setEmptyTimeout(10) ->setMaxParticipants(20); $room = $svc->createRoom($opts); // Delete a room. $svc->deleteRoom('myroom');
Running Tests
We'll utilize Lando to streamline the test execution process. However, should you choose to run the tests on your local environment directly, you can certainly proceed with that approach.
Step 1:
Generate your environment file by duplicating example.env and renaming the copy to .env, then enter your credentials
accordingly.
Step 2:
Start the lando project.
lando start
Step 3:
Run the tests. This single command handles everything automatically: creating a test room, starting load test participants, setting up an RTMP tunnel, running PHPUnit, and cleaning up when done.
lando test
| LiveKit Ecosystem | |
|---|---|
| LiveKit SDKs | Browser · iOS/macOS/visionOS · Android · Flutter · React Native · Rust · Node.js · Python · Unity · Unity (WebGL) · ESP32 |
| Server APIs | Node.js · Golang · Ruby · Java/Kotlin · Python · Rust · PHP (community) · .NET (community) |
| UI Components | React · Android Compose · SwiftUI |
| Agents Frameworks | Python · Node.js · Playground |
| Services | LiveKit server · Egress · Ingress · SIP |
| Resources | Docs · Example apps · Cloud · Self-hosting · CLI |