Delphi WebRTC Library — P2P, ICE, STUN, TURN, DataChannel

A native Pascal implementation of the WebRTC peer-to-peer stack: RTCPeerConnection, ICE candidate gathering and connectivity checks, STUN and TURN client + server, DTLS-SRTP key agreement and SCTP data channels — with WebSocket signalling already wired in.

Peer-to-peer for Pascal, end to end

Bring browser-grade P2P to Delphi — without bundling Chromium.

A Delphi WebRTC library lets two Delphi processes (or a Delphi process and a browser) establish a direct, NAT-traversed, end-to-end-encrypted channel without routing payload through a central server. sgcWebSockets provides every WebRTC building block as a Pascal component: TsgcWSPClient_WebRTC mirrors the JavaScript RTCPeerConnection API, TsgcSTUNClient / TsgcSTUNServer implement RFC 5389, TsgcTURNClient / TsgcTURNServer implement RFC 5766, and a complete ICE agent ties them together.

Unlike browser-based stacks (which require Chromium or libwebrtc — tens of megabytes of native code and a complicated build) the sgcWebSockets implementation is pure Pascal on top of OpenSSL and is compiled into your binary. It runs on Delphi 7 through Delphi 13 and ships native binaries for Win32/Win64, Linux64, macOS, iOS and Android.

Peer connection

TsgcWSPClient_WebRTC

ICE / STUN / TURN

P2P components

Standards

RFC 8825 / 5245 / 5389 / 5766 / 6347 / 4960

Edition

Enterprise

Every WebRTC component, exposed

The peer connection is the headline, but the supporting pieces are first-class components too.

RTCPeerConnection

TsgcWSPClient_WebRTC mirrors the JavaScript API: CreateOffer, CreateAnswer, SetLocalDescription, SetRemoteDescription, AddIceCandidate, CreateDataChannel. See RTCPeerConnection.

SDP offer / answer

Built-in SDP serialiser and parser. Generate offers, accept answers, trickle ICE candidates with the same wire format Chrome and Firefox produce.

ICE agent

Full candidate gathering: host, server-reflexive (STUN-discovered), relayed (TURN-allocated). Pairing, prioritisation and connectivity checks per RFC 5245. ICE page.

STUN client + server

Stand-alone STUN client and STUN server for NAT discovery and keep-alive. Run your own STUN endpoint with two lines of Pascal.

TURN client + server

TURN client for relay allocation and TURN server for self-hosting. Long-term credentials, IPv4 + IPv6.

DTLS-SRTP

The mandatory WebRTC key-agreement protocol. DTLS 1.2 handshake over the same ICE-negotiated UDP path, exporting keying material for SRTP.

SCTP data channels

Reliable / unreliable, ordered / unordered messages over the WebRTC media transport — the standard way to send arbitrary application data peer-to-peer.

WebSocket signalling

Use the bundled TsgcWebSocketClient / TsgcWebSocketHTTPServer as your signalling channel — one library for everything.

WebSocket signalling: the standard pattern

WebRTC does not define a signalling protocol — you bring your own. The conventional choice is a WebSocket channel that carries three message types: offer (SDP from the caller), answer (SDP from the callee) and ice-candidate (candidates trickled by either side). sgcWebSockets includes both ends of the signalling server already: TsgcWebSocketHTTPServer with a Channels implementation that routes JSON messages between matched peers.

Because the peer-connection component and the signalling server live in the same library, you can stand up a complete WebRTC application — coordinated discovery, NAT traversal, encrypted P2P payload — without integrating a single third-party SDK.

Configuring ICE servers

The same IceServers collection a browser uses — with both public servers and your own private TURN.

uses
  sgcWebSocket, sgcWebSocket_Protocol_WebRTC_Client,
  sgcWebSocket_Protocol_WebRTC_Types;

var
  WSClient: TsgcWebSocketClient;
  RTC: TsgcWSPClient_WebRTC;
begin
  WSClient := TsgcWebSocketClient.Create(nil);
  WSClient.URL := 'wss://signal.example.com/rtc';

  RTC := TsgcWSPClient_WebRTC.Create(nil);
  RTC.Client := WSClient;

  // Public Google STUN for NAT discovery
  RTC.IceServers.Add.URL := 'stun:stun.l.google.com:19302';

  // Your own TURN for relay when P2P fails
  with RTC.IceServers.Add do
  begin
    URL        := 'turn:turn.example.com:3478';
    UserName   := 'alice';
    Credential := 's3cret';
  end;

  RTC.OnDataChannelMessage := DoMessage;

  WSClient.Active := True;

  // Create a reliable, ordered data channel
  RTC.CreateDataChannel('chat', True, True);
  RTC.CreateOffer;
end;

procedure TForm1.DoMessage(Sender: TObject;
  const aChannel, aText: string);
begin
  Memo1.Lines.Add(aChannel + ': ' + aText);
end;

// Send a message peer-to-peer
RTC.SendDataChannelMessage('chat', 'Hello from Delphi');

Run your own STUN / TURN in Delphi

Most teams start with public Google STUN and a third-party TURN service (Twilio, Xirsys, coturn). When traffic ramps up, the relay bandwidth bill ramps up too — and TURN is, in practice, the only piece of WebRTC where you pay per-byte. The library lets you self-host: TsgcSTUNServer and TsgcTURNServer drop into a console application or Windows service and serve the protocol on UDP 3478 (STUN) and TCP/UDP/TLS on configurable ports for TURN. Long-term credentials, IPv4 and IPv6 dual-stack, allocation quotas and per-user bandwidth limits are all built in.

More on P2P in sgcWebSockets

P2P / WebRTC hub

Landing page for every peer-to-peer component.

RTCPeerConnection

Detailed component reference.

ICE agent

Candidate gathering and connectivity checks.

STUN & TURN

NAT discovery and relay servers.

Blog: RTCPeerConnection P2P

End-to-end walkthrough of a Delphi-to-Delphi data channel.

Blog: STUN + TURN server & client

Self-hosting your own STUN/TURN infrastructure in Delphi.

Blog: coturn on Windows

Cross-referencing the reference C implementation for interoperability tests.

Build your first P2P channel

Download the trial — the WebRTC, STUN and TURN demos ship as compilable Delphi projects.