0.0
No release in over 3 years
WebRTC (Web Real-Time Communication) bindings for Ruby, providing real-time audio, video, and data channel capabilities.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 1.15
 Project Readme

WebRTC Ruby

WebRTC bindings for Ruby, providing real-time audio, video, and data channel capabilities.

Status

This project provides a practical, webrtc-rs-compatible API surface in Ruby, with a mix of:

  • native-backed features through libdatachannel C API
  • Ruby compatibility/typing layers for API parity

Native-backed (current)

  • WebRTC.init / WebRTC.cleanup
  • RTCPeerConnection
    • offer/answer
    • local/remote description
    • ICE candidate add
    • connection/ICE/signaling/gathering states + callbacks
    • data channel callbacks
    • track callbacks
    • add/remove track path (via native track ID)
  • RTCDataChannel
    • create/close/send
    • open/message/close callbacks
    • buffered amount
  • RTCSessionDescription
  • RTCIceCandidate

Compatibility and typed layer (current)

  • Factory API
    • CurrentTaskQueueFactory
    • PeerConnectionFactory
    • WebRTC.create_modular_peer_connection_factory
  • Observer API
    • CreateSessionDescriptionObserver
    • SetLocalDescriptionObserver
    • SetRemoteDescriptionObserver
    • AddIceCandidateObserver
    • DataChannelObserver
    • RTCStatsCollectorCallback
  • Typed parity classes
    • CreateOfferOptions, CreateAnswerOptions
    • DataChannelInit, DataChannelMessage
    • RTP parameter/capability classes
    • RTCError and enum modules
    • media interface classes
    • VideoFrame, I420Buffer

Stats note

  • RTCStatsCollectorCallback and RTCStatsResponse are wired.
  • DataChannel message/byte counters are measured from actual send/receive events.
  • Low-level RTP/ICE transport metrics remain limited by libdatachannel C API exposure.

Requirements

  • Ruby 3.1+
  • CMake
  • OpenSSL development libraries

Installation

Add this line to your application's Gemfile:

gem 'webrtc-ruby'

Then run:

bundle install

Building Native Extension

The gem requires a native extension (ext/webrtc_ruby) and libdatachannel.

macOS

brew install cmake openssl

# Clone and build libdatachannel
git clone --depth 1 --recurse-submodules https://github.com/paullouisageneau/libdatachannel.git vendor/libdatachannel
cd vendor/libdatachannel
mkdir build && cd build
cmake -DUSE_GNUTLS=0 -DUSE_NICE=0 -DNO_WEBSOCKET=1 -DNO_EXAMPLES=1 -DNO_TESTS=1 ..
make -j$(sysctl -n hw.ncpu)
cd ../../..

# Build extension
cd ext/webrtc_ruby
mkdir -p build && cd build
cmake ..
make

Linux (Ubuntu/Debian)

sudo apt install build-essential cmake git libssl-dev pkg-config

# Clone and build libdatachannel
git clone --depth 1 --recurse-submodules https://github.com/paullouisageneau/libdatachannel.git vendor/libdatachannel
cd vendor/libdatachannel
mkdir build && cd build
cmake -DUSE_GNUTLS=0 -DUSE_NICE=0 -DNO_WEBSOCKET=1 -DNO_EXAMPLES=1 -DNO_TESTS=1 ..
make -j$(nproc)
cd ../../..

# Build extension
cd ext/webrtc_ruby
mkdir -p build && cd build
cmake ..
make

Quick Start

require 'webrtc'

WebRTC.init

pc1 = WebRTC::RTCPeerConnection.new
pc2 = WebRTC::RTCPeerConnection.new

pc1_candidates = []
pc2_candidates = []

pc1.on_ice_candidate { |c| pc1_candidates << c if c }
pc2.on_ice_candidate { |c| pc2_candidates << c if c }

pc2.on_data_channel do |dc|
  dc.on_message { |msg| puts "pc2 recv: #{msg.data}" }
end

dc1 = pc1.create_data_channel('chat')
dc1.on_open { puts 'pc1 data channel open' }
dc1.on_message { |msg| puts "pc1 recv: #{msg.data}" }

offer = pc1.create_offer.await
pc1.set_local_description(offer).await
pc2.set_remote_description(offer).await

answer = pc2.create_answer.await
pc2.set_local_description(answer).await
pc1.set_remote_description(answer).await

pc1_candidates.each { |c| pc2.add_ice_candidate(c).await rescue nil }
pc2_candidates.each { |c| pc1.add_ice_candidate(c).await rescue nil }

pc1.close
pc2.close
WebRTC.cleanup

Factory Example

factory = WebRTC.create_modular_peer_connection_factory
pc = factory.create_peer_connection(
  ice_servers: [{ urls: 'stun:stun.l.google.com:19302' }]
)

Observer Example

observer = WebRTC::CreateSessionDescriptionObserver.new(
  on_success: ->(desc) { puts "created: #{desc.type}" },
  on_failure: ->(err) { warn err.message }
)

pc.create_offer(observer: observer).await

Stats Example

report = pc.get_stats.await

report.each do |_id, stat|
  case stat
  when WebRTC::RTCDataChannelStats
    puts "dc bytes sent=#{stat.bytes_sent} recv=#{stat.bytes_received}"
  when WebRTC::RTCTransportStats
    puts "transport bytes sent=#{stat.bytes_sent} recv=#{stat.bytes_received}"
  end
end

API Coverage (shiguredo/webrtc-rs parity)

Area Status Notes
PeerConnection core Implemented state callbacks, data channel callbacks, track callbacks
Factory API Implemented CurrentTaskQueueFactory, PeerConnectionFactory, create_modular_peer_connection_factory
SDP Observer API Implemented create/set description observers and option classes
ICE Candidate Observer API Implemented RTCIceCandidateInit, AddIceCandidateObserver
DataChannel typed API Implemented DataChannelInit, DataChannelObserver, DataChannelMessage, state constants
RTP Sender/Receiver/Transceiver Implemented native add/remove track path + typed parameter classes
Media interfaces Implemented audio/video track/source/sink interfaces
Video frame API Implemented VideoFrame, I420Buffer
Error and helper types Implemented RTCError, RTCCertificate, RTCEncodedImage, enum modules
Stats callback API Partial DataChannel bytes/messages are measured; low-level RTP/ICE metrics are constrained by libdatachannel C API

Development

After checking out the repo, install dependencies:

bundle install

Important note about libdatachannel

  • Building the native extension and running tests require libdatachannel.
  • You must use one of these approaches before testing:
# Option A (recommended): keep a local vendor copy
git clone --depth 1 --recurse-submodules https://github.com/paullouisageneau/libdatachannel.git vendor/libdatachannel
cd vendor/libdatachannel
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DNO_EXAMPLES=ON -DNO_TESTS=ON
cmake --build .
cd ../../..
# Option B: install libdatachannel system-wide (pkg-config discoverable)
# In this case CMake falls back to pkg-config when vendor/libdatachannel is absent.

Run tests

# Build native extension first
cd ext/webrtc_ruby
mkdir -p build && cd build
cmake ..
cmake --build .
cd ../../..

# Run test suite (macOS)
DYLD_LIBRARY_PATH="ext/webrtc_ruby/build:vendor/libdatachannel/build" bundle exec rspec

# Run test suite (Linux)
LD_LIBRARY_PATH="ext/webrtc_ruby/build:vendor/libdatachannel/build" bundle exec rspec

License

This project is available under the Apache License 2.0.

Contributing

Bug reports and pull requests are welcome at: