Skip to content

LivekitVideochat

Peter Veitz edited this page Oct 4, 2024 · 1 revision

How to set up servers for the Livekit video chat

This guide covers how to set up two servers needed for the new video chat system:

  1. OpenVidu or Livekit Server for handling video streams.
  2. Node.js Server for generating authentication tokens to join the video chat.

Note: You can choose either OpenVidu or Livekit as your video stream server. You do not need to set up both. If you're unable to use Docker, Livekit is a good alternative to OpenVidu.

Setting up OpenVidu server (locally)

To set up an OpenVidu server on your local machine, follow the official guide provided here: OpenVidu Local Self-Hosting Guide.

Prerequisites:

  • Docker and Docker Compose installed on your machine.

Installation Steps:

  1. Clone the following repository:
git clone https://github.com/OpenVidu/openvidu-local-deployment
  1. Configure the local deployment:
cd openvidu-local-deployment/community
./configure_lan_private_ip_linux.sh
  1. Run OpenVidu:
docker compose up

Setting up Livekit server (locally)

To set up a Livekit server on your local machine, follow the official guide provided here: Livekit Server Setup Guide.

  1. Install Livekit Server on Linux with:
curl -sSL https://get.livekit.io | bash
  1. You can start LiveKit in development mode by running:
livekit-server --dev

Setting up the Node.js token server

You’ll need a Node.js server to generate tokens for clients to join rooms on the video chat. The following steps will guide you through creating a basic Node.js server using express and livekit-server-sdk.

Note: For more detailed information, you can refer to the official documentation on generating tokens here: Livekit Token Generation Guide.

  1. Install Node.js and npm
  2. Create a new directory for the Node.js server:
mkdir livekit-token-server
cd livekit-token-server
  1. Initialize a new Node.js project:
npm init -y
  1. Install necessary dependencies:
npm install express dotenv
npm install livekit-server-sdk --save
  1. Create the server.js file: In the root directory, create a server.js file with the following content:
import express from 'express';
import { AccessToken } from 'livekit-server-sdk';

const createToken = async (roomName, participantName) => {
  const at = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, {
    identity: participantName,
    ttl: '1m', // Token expires after 1 minute
  });
  at.addGrant({ roomJoin: true, room: roomName });

  return await at.toJwt();
}

const app = express();
const port = 3000;

app.get('/getToken', async (req, res) => {
  const roomName = req.query.roomName;
  const participantName = req.query.participantName;
  res.send(await createToken(roomName, participantName));
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});
  1. Create the .env file:
LIVEKIT_API_KEY=devkey
LIVEKIT_API_SECRET=secret
  1. Run the Node.js server:
source .env
node server.js

Test your servers

Once both servers are set up and running, you can test their functionality by following these steps:

  1. Use your Node.js token server to generate a token by making a GET request in your browser or via curl: http://localhost:3000/getToken?roomName=testRoom&participantName=JohnDoe
  2. Open your browser and visit: https://meet.livekit.io/?tab=custom

In the Custom Server tab:

  • Enter the URL of your OpenVidu or Livekit server.
  • Use the token generated in step 1 to join the room.
Clone this wiki locally