Overview
This guide walks you through creating a simple WebRTC data channel application. You’ll build a Go server that:- Creates a PeerConnection
- Establishes a DataChannel
- Exchanges messages with a web browser
- Handles ICE candidates and signaling
What You’ll Build
By the end of this guide, you’ll have a working WebRTC server that:- Accepts connections from web browsers
- Creates bidirectional data channels
- Sends and receives text messages in real-time
- Handles ICE gathering and signaling automatically
Prerequisites
Before starting, make sure you have:- Go 1.24.0 or later installed
- Pion WebRTC v4 installed (Installation Guide)
- Basic understanding of Go
- A web browser for testing
Step 1: Create Your Project
Step 2: Create the Server
Create a file namedmain.go with the following code:
Step 3: Understanding the Code
Let’s break down the key components:PeerConnection Configuration
PeerConnection Configuration
Configuration specifies STUN servers for NAT traversal. Google’s public STUN server helps establish connections through firewalls.DataChannel Event Handlers
DataChannel Event Handlers
Offer/Answer Exchange
Offer/Answer Exchange
ICE Candidate Handling
ICE Candidate Handling
Step 4: Run Your Application
Open in Browser
Navigate to http://localhost:8080 in your web browser.
Expected Output
When you connect, the server terminal will show:More Complex Example: Pion-to-Pion
For server-to-server communication, check out this example from the Pion repository:Key Concepts Explained
PeerConnection Lifecycle
Monitor the connection state:
DataChannel Options
Customize DataChannel behavior:DataChannel Types:
- Ordered + Reliable - Guaranteed delivery in order (default)
- Unordered + Unreliable - Best for real-time gaming, live streams
- Partially Reliable - Balance between reliability and latency
Signaling Patterns
This quickstart uses HTTP for signaling, but production apps typically use:- WebSocket - For real-time bidirectional signaling
- WHIP/WHEP - Standardized HTTP-based signaling
- Custom Protocol - Over any transport (Redis, MQTT, etc.)
Next Steps
Send Media
Learn to send audio and video from files or live sources
Receive Media
Capture and save incoming audio/video streams
Advanced Examples
Explore simulcast, renegotiation, stats, and more
API Reference
Deep dive into the complete Pion WebRTC API
Common Patterns
Broadcasting to Multiple Peers
Handling Connection Failures
Binary Data Transfer
Troubleshooting
Connection stays in 'connecting' state
Connection stays in 'connecting' state
- Verify STUN server is reachable
- Check firewall settings
- Ensure ICE candidates are being exchanged
- Try adding a TURN server for relay
DataChannel never opens
DataChannel never opens
- Confirm both sides have completed SDP exchange
- Check that SetRemoteDescription was called
- Verify ICE gathering completed before exchanging SDP
- Look for errors in OnDataChannel handler
Messages not being received
Messages not being received
- Ensure DataChannel.OnMessage is set before channel opens
- Check DataChannel.ReadyState (should be ‘open’)
- Verify no errors from SendText/Send
- Check for SCTP congestion or flow control
High latency or packet loss
High latency or packet loss
- Use unordered, unreliable channels for real-time data
- Consider adjusting MaxRetransmits
- Check network conditions with stats
- Monitor DataChannel.BufferedAmount
Learn More
WebRTC for the Curious
Understand WebRTC protocols at a deep level: ICE, DTLS, SCTP, RTP, and more
- Discord: discord.gg/PngbdqpFbt
- GitHub: github.com/pion/webrtc
- Examples: github.com/pion/webrtc/tree/master/examples