Skip to main content

Introduction

Pion WebRTC is a pure Go implementation of the WebRTC API. This reference documentation covers the core types and methods for building real-time peer-to-peer communication applications.

Core Components

The Pion WebRTC API is organized around several key components:

API Configuration

The foundation of customizing WebRTC behavior:

API

Configure PeerConnection with custom SettingEngine, MediaEngine, and Interceptors

Peer Connection

The central interface for WebRTC connections:

PeerConnection

Establish peer-to-peer communications with media and data channels

Configuration

Define connection parameters:

Configuration

Configure ICE servers, transport policies, and certificates

Session Description

Manage SDP offers and answers:

SessionDescription

Handle SDP negotiation for establishing connections

Architecture

Pion WebRTC follows the standard WebRTC architecture:
┌─────────────────────────────────────────┐
│           PeerConnection                │
├─────────────────────────────────────────┤
│  • RTP Transceivers (Media)             │
│  • Data Channels (SCTP)                 │
│  • ICE Transport (Connectivity)         │
│  • DTLS Transport (Security)            │
└─────────────────────────────────────────┘

Quick Start

package main

import (
    "github.com/pion/webrtc/v4"
)

func main() {
    // Create a new API with default settings
    api := webrtc.NewAPI()
    
    // Configure the connection
    config := webrtc.Configuration{
        ICEServers: []webrtc.ICEServer{
            {
                URLs: []string{"stun:stun.l.google.com:19302"},
            },
        },
    }
    
    // Create a new PeerConnection
    pc, err := api.NewPeerConnection(config)
    if err != nil {
        panic(err)
    }
    defer pc.Close()
    
    // Add event handlers
    pc.OnICECandidate(func(c *webrtc.ICECandidate) {
        if c != nil {
            // Send candidate to remote peer
        }
    })
}

Key Concepts

Signaling

Pion WebRTC handles media transport but requires external signaling to exchange:
  • Session descriptions (SDP offers/answers)
  • ICE candidates
  • Connection state

Media Handling

The API provides:
  • RTPTransceiver: Bidirectional media streams
  • RTPSender: Outbound media
  • RTPReceiver: Inbound media
  • TrackLocal/TrackRemote: Audio/video tracks

Data Channels

For non-media communication:
  • Reliable or unreliable delivery
  • Ordered or unordered messages
  • Custom protocols

Common Patterns

offer, err := pc.CreateOffer(nil)
if err != nil {
    return err
}

err = pc.SetLocalDescription(offer)
if err != nil {
    return err
}

// Send offer to remote peer via signaling
// Receive answer from remote peer
answer := webrtc.SessionDescription{
    Type: webrtc.SDPTypeAnswer,
    SDP:  answerSDP,
}

err := pc.SetRemoteDescription(answer)
if err != nil {
    return err
}
// Create a video track
videoTrack, err := webrtc.NewTrackLocalStaticSample(
    webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8},
    "video",
    "pion",
)
if err != nil {
    return err
}

// Add track to connection
sender, err := pc.AddTrack(videoTrack)
if err != nil {
    return err
}
// Create a data channel
dc, err := pc.CreateDataChannel("chat", nil)
if err != nil {
    return err
}

dc.OnOpen(func() {
    dc.SendText("Hello!")
})

dc.OnMessage(func(msg webrtc.DataChannelMessage) {
    // Handle message
})

Next Steps

API Configuration

Learn how to configure the API with custom engines and interceptors

PeerConnection

Explore PeerConnection methods and event handlers

Configuration

Configure ICE servers, policies, and certificates

SessionDescription

Understand SDP types and session negotiation

Source Reference

All API documentation is extracted from the actual Pion WebRTC source code:
  • Package: github.com/pion/webrtc/v4
  • License: MIT
  • Copyright: 2026 The Pion community

Build docs developers (and LLMs) love