Build a Full WebRTC Stack with Pure PHP – No Node.js Needed

This guide shows how to implement a complete WebRTC solution using only PHP with FFI, covering dependency installation, Composer setup, and detailed RTCPeerConnection configuration including STUN/TURN servers and TLS certificates.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Build a Full WebRTC Stack with Pure PHP – No Node.js Needed

Overview

The PHP‑WebRTC project implements the full WebRTC protocol stack in pure PHP, removing the need for Node.js or other JavaScript runtimes. It supports ICE, DTLS, SRTP, SCTP, RTP and data channels, enabling real‑time audio, video and data streams through a modular, ReactPHP‑based asynchronous architecture. The library requires PHP FFI to be enabled.

Dependency installation script

Create a file named install-deps.sh and paste the following Bash script. The script installs system packages, PHP extensions, and builds the native media libraries required by the WebRTC stack.

#!/bin/bash
set -e

# Update package index and install base development tools
apt-get update && apt-get install -y \
    apt-transport-https ca-certificates gnupg autoconf automake build-essential \
    cmake git libtool pkg-config yasm nasm wget unzip curl \
    libzip-dev libgmp-dev libssl-dev libsrtp2-dev libx264-dev libffi-dev \
    libprotobuf-dev php-dev php-pear php-gmp php-zip php-ffi php-cli php-mbstring \
    php-curl php-xml php-bcmath php-tokenizer php-dom php-pcov php-sqlite3 \
    php-pdo php-json php-opcache php-readline php-soap php-intl php-exif php-fileinfo \
    php-phar php-fpm php-common php-iconv php-posix php-sockets php-pcntl python3 && \
    rm -rf /var/lib/apt/lists/*

echo "✅ Base system dependencies installed."

# Install PHP Protobuf extension via PECL
pecl install protobuf
echo "extension=protobuf.so" > /etc/php/8.4/cli/conf.d/30-protobuf.ini
echo "✅ Protobuf extension installed and enabled."

# Enable PHP FFI
echo "ffi.enable=true" > /etc/php/8.4/cli/conf.d/30-ffi.ini
echo "✅ FFI enabled."

# Build and install FFmpeg 7.1.1 with required codecs
cd /tmp
wget https://ffmpeg.org/releases/ffmpeg-7.1.1.tar.bz2
tar xjf ffmpeg-7.1.1.tar.bz2
cd ffmpeg-7.1.1
./configure --enable-shared --enable-gpl --enable-libx264 --enable-libopus --enable-libvpx
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf ffmpeg-7.1.1*
echo "✅ FFmpeg 7.1.1 installed."

# Build and install libopus 1.4
cd /tmp
wget https://github.com/xiph/opus/releases/download/v1.4/opus-1.4.tar.gz
tar xzvf opus-1.4.tar.gz
cd opus-1.4
./configure --prefix=/usr/local --enable-shared
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf opus-1.4*
echo "✅ libopus 1.4 installed."

# Build and install libvpx 1.15.0
cd /tmp
wget https://github.com/webmproject/libvpx/archive/refs/tags/v1.15.0.tar.gz
tar xzvf v1.15.0.tar.gz
cd libvpx-1.15.0
./configure --prefix=/usr/local --enable-shared --disable-examples
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf libvpx-1.15.0*
echo "✅ libvpx 1.15.0 installed."

echo "🎉 All dependencies installed successfully!"

Running the script

chmod +x install-deps.sh
./install-deps.sh

Composer installation

Install the PHP WebRTC library via Composer:

composer require quasarstream/webrtc

RTCPeerConnection configuration

The RTCConfiguration class defines the options for an RTCPeerConnection, including ICE/TURN servers and optional TLS certificate and private‑key paths for secure DTLS connections.

use Webrtc\Webrtc\RTCConfiguration;
use Webrtc\Webrtc\ICE\RTCIceServer;

$stunServer = new RTCIceServer();
$stunServer->setUrls(['stun:stun.l.google.com:19302']);

$turnServer = new RTCIceServer();
$turnServer->setUrls(['turn:turn.example.com']);
$turnServer->setUsername('user');
$turnServer->setCredential('pass');
$turnServer->setCredentialType('password');

$config = new RTCConfiguration(
    iceServers: [$stunServer, $turnServer],
    certificatePath: '/etc/ssl/certs/rtc-cert.pem',
    privateKeyPath: '/etc/ssl/private/rtc-key.pem'
);

A default configuration that uses Google’s public STUN server can be created without explicit parameters:

$configuration = new RTCConfiguration();
$pc = new RTCPeerConnection($configuration);
// or simply
$pc = new RTCPeerConnection();

Alternatively, pass an associative array directly to the RTCPeerConnection constructor:

use Webrtc\Webrtc\RTCConfiguration;

$pc = new RTCPeerConnection([
    'iceServers' => [
        [
            'urls' => ['stun:stun.l.google.com:19302']
        ],
        [
            'urls' => ['turn:turn.example.com'],
            'username' => 'user',
            'credential' => 'pass',
            'credentialType' => 'password'
        ]
    ],
    'certificatePath' => '/etc/ssl/certs/rtc-cert.pem',
    'privateKeyPath' => '/etc/ssl/private/rtc-key.pem'
]);

For the full source code and additional documentation, see the repository at https://github.com/PHP-WebRTC.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

videoRTPWebRTC
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.