Git Product home page Git Product logo

livekit-server-sdk-php's Introduction

LiveKit Server API for PHP

PHP APIs to manage rooms and to create access tokens. This library is designed to work with livekit-server. Use it with a PHP backend to manage access to LiveKit.

Installation

Requirements

  • php: >= 8

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_HOST
  • LIVEKIT_API_KEY
  • LIVEKIT_API_SECRET

Creating Access Tokens

Creating a token for participant to join a room.

// 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.

// 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:

$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.

$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.dev and renaming the copy to .env, then enter your credentials accordingly.

Step 2:

Start the lando project.

lando start

Step 3:

Set up a LiveKit project with the livekit-cli by executing the command below, which will prompt you to provide some API credentials.

lando create-project

Step 4:

Generate the LiveKit room that will serve as the testing environment for the majority of the test cases.

lando create-test-room

Step 5:

Initialize 5 test users within the room. Run this command in a separate terminal window.

lando start-test-users

Step 6:

Time to get busy testing.

lando test

Step 7:

Once tests are completed, it is time to clean up.

  • End the lando start-test-users command.
  • Run lando delete-test-room to delete the test room.

livekit-server-sdk-php's People

Contributors

p-delorme avatar ashwin-nath-m avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.