Skip to content

LivekitVideochat

Rainer Koschke edited this page Jan 11, 2025 · 4 revisions

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.

Install Livekit Server

Linux

You can download the LiveKit Server on Linux as follows:

curl -sSL https://get.livekit.io | bash

Windows

You can download the LiveKit Server here, choosing between the ARM or AMD. Your system architecture can be found in Windows under Settings => System => About => Device Specification => System type.

Unpack the downloaded ZIP archive and copy the exectuable livekit-server contained therein wherever you want.

Start LiveKit Server

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.mjs file: In the root directory, create a server.mjs file with the following content (mind the file extension .mjs; if you are using .js you might get a complain about a syntax error when you execute this file as described in the section below):
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. Set the necessary environment variables:

On Linux:

export LIVEKIT_API_KEY=devkey
export LIVEKIT_API_SECRET=secret

On Windows:

set "LIVEKIT_API_KEY=devkey"
set "LIVEKIT_API_SECRET=secret"
  1. Run the Node.js server:
node server.mjs

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.