..

Setting up MatrixRTC for Continuwuity with LiveKit and lk-jwt-service

This guide assumes that you already have set up a working Continuwuity matrix homeserver using Caddy as your reverse proxy. It should, however, be generally applicable to anyone who wishes to set up lk-jwt-service and LiveKit for MatrixRTC without the use of Docker.

While setting it up for myself, I was referencing Continuwuity’s MatrixRTC docs and Kimiblock Moe’s blog, and naturally have taken inspiration from both to create this guide.

If you have any issues with your set up, the folks in #continuwuity:continuwuity.org will be more than willing to help you out. If you have a question about this guide specifically, feel free to reach out to me on matrix or via email at the bottom of this page.

Setting up LiveKit

LiveKit

Installing LiveKit

For MatrixRTC to work, you will need to install two more pieces of software, lk-jwt-service and LiveKit. In order to install LiveKit, you can run curl -sSL https://get.livekit.io | sudo bash, which will install the LiveKit server to /usr/local/bin/livekit-server

Configuring LiveKit

Then you will have to generate your API key that lk-jwt-service will use for authenticating itself with LiveKit. You can do this by running:

livekit-server generate-keys
# API Key:  APIzsXgrC7bMwbN #This will be your LK_MATRIX_KEY
# API Secret:  ZSdSb0GcrBfvaAsY87I1MFerZVyxO5X3AXTjwytnA3O #This will be your LK_MATRIX_SECRET

Next you’ll have to configure LiveKit: Create a configuration file at /etc/livekit/config.yaml, and secure it with the following command: sudo chown root:root /etc/livekit/config.yaml && sudo chmod 600 /etc/livekit/config.yaml. Here is what my configuration file looks like. You may wish to take a look at LiveKit’s sample config to see the other options.

port: 7880
bind_addresses:
  - "127.0.0.1"
rtc:
  tcp_port: 7881
  port_range_start: 50100
  port_range_end: 50200
  use_external_ip: true
  enable_loopback_candidate: false
keys:
  LK_MATRIX_KEY: LK_MATRIX_SECRET
  # replace these with your key-secret pair. Example:
  # APIUxUnMnSkuFWV: t93ZVjPeoEdyx7Wbet3kG4L3NGZIZVEFvqe0UuiVc22A

# do not create rooms by default
room:
  auto_create: false

You’ll also have to allow those ports for LiveKit to function. Using UFW this can be accomplished with ufw allow 7881/tcp and ufw allow 50100:50200/udp.

Systemd service

In order to have LiveKit run in the background and start at boot, you can set up a systemd service: run systemctl edit --force --full livekit and create your service. Below is what mine looks like (mostly copied from Kimiblock Moe):

[Unit]
Description="LiveKit: Real-time video, audio and data for developers"
After=network.target
Documentation=https://docs.livekit.io/home/

[Service]
DynamicUser=yes
User=livekit
Group=livekit
LoadCredential=config.yaml:/etc/livekit/config.yaml #this allows LiveKit to access the root owned config file as read only

ExecStart=/usr/local/bin/livekit-server --config ${CREDENTIALS_DIRECTORY}/config.yaml --node-ip <YOUR IP ADDR>

Restart=always
RestartSec=5

TimeoutStopSec=4m
TimeoutStartSec=4m
StartLimitInterval=1m
StartLimitBurst=5

AmbientCapabilities=
CapabilityBoundingSet=

DevicePolicy=closed
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
#ProcSubset=pid
ProtectClock=yes
ProtectControlGroups=yes
#ProtectHome=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
#ProtectProc=invisible
ProtectSystem=strict
PrivateDevices=yes
PrivateMounts=yes
PrivateTmp=yes
PrivateIPC=yes
RemoveIPC=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes

SystemCallFilter=~@clock @debug @module @mount @reboot @swap @cpu-emulation @obsolete @timer @chown @setuid @privileged @keyring
SystemCallErrorNumber=EPERM

[Install]
WantedBy=multi-user.target

Setting up your reverse proxy

As previously mentioned, this section will focus on Caddy, if you are using NGINX, Kimiblock Moe has an example there, though I have not tested it.

Add this section to your caddyfile, changing the ports if need be:

livekit.example.com {
    # for livekit
    reverse_proxy 127.0.0.1:7880
}

lk-jwt-service

Installing lk-jwt-service

To install lk-jwt-service, you can run wget -q https://github.com/element-hq/lk-jwt-service/releases/download/v0.10.0/lk-jwt-service_linux_amd64 && chmod +x lk-jwt-service_linux_amd64 && sudo cp lk-jwt-service_linux_amd64 /usr/local/bin/lk-jwt-service, changing lk-jwt-service_linux_amd64 to lk-jwt-service_linux_arm64 if that is your host architecture. Do note that not running as an application service (which Continuwuity doesn’t support at this moment) is currently deprecated, so be careful when installing a version later than v0.10.0 that it still supports this method.

Systemd service

As before, run systemctl edit --force --full lk-jwt-service. Here is what mine looks like:

[Unit]
Description="LiveKit Management Service"
After=network.target

[Service]
DynamicUser=yes

Environment=LIVEKIT_URL="wss://livekit.example.com" #this should match what you use in your reverse proxy
Environment=LIVEKIT_JWT_BIND=":27881"
Environment=LIVEKIT_KEY='LK_MATRIX_KEY'
Environment=LIVEKIT_SECRET="LK_MATRIX_SECRET"
Environment=HOMESERVER_URL="https://example.com"
Environment=LIVEKIT_FULL_ACCESS_HOMESERVERS="example.com"

ExecStart=/usr/local/bin/lk-jwt-service

Restart=on-failure
RestartSec=5

TimeoutStopSec=4m
TimeoutStartSec=4m
StartLimitInterval=1m
StartLimitBurst=5

AmbientCapabilities=
CapabilityBoundingSet=

DevicePolicy=closed
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
ProcSubset=pid
ProtectClock=yes
ProtectControlGroups=yes
ProtectHome=yes
ProtectHostname=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
ProtectProc=invisible
ProtectSystem=strict
PrivateDevices=yes
PrivateMounts=yes
PrivateTmp=yes
PrivateUsers=yes
PrivateIPC=yes
RemoveIPC=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes

SystemCallFilter=~@clock @debug @module @mount @reboot @swap @cpu-emulation @obsolete @timer @chown @setuid @privileged @keyring
SystemCallErrorNumber=EPERM


[Install]
WantedBy=multi-user.target

Add to your reverse proxy

Add to your previous block in your Caddyfile. Here is what my complete version looks like:

livekit.example.com {

    # for lk-jwt-service
    @lk-jwt-service path /sfu/get* /healthz* /get_token*
    route @lk-jwt-service {
        reverse_proxy 127.0.0.1:27881
    }

    # for livekit
    reverse_proxy 127.0.0.1:7880
}

Setting up Continuwuity

Here you can just follow the instructions in Continuwuity’s docs

Start it up

You should now be set up fully for everything, run these command to enable and start your services:

sudo systemctl daemon-reload
sudo systemctl enable --now livekit lk-jwt-service
sudo systemctl reload caddy

Reply via email (English or Français)