Skip to main content

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
Data channels are perfect for real-time text communication, gaming, file transfer, or any low-latency data exchange.

What You’ll Build

By the end of this guide, you’ll have a working WebRTC server that:
  1. Accepts connections from web browsers
  2. Creates bidirectional data channels
  3. Sends and receives text messages in real-time
  4. 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

1

Initialize Project

2

Install Pion WebRTC

Step 2: Create the Server

Create a file named main.go with the following code:

Step 3: Understanding the Code

Let’s break down the key components:
The Configuration specifies STUN servers for NAT traversal. Google’s public STUN server helps establish connections through firewalls.
Event handlers respond to channel lifecycle events and incoming messages.
This implements the SDP offer/answer exchange for WebRTC signaling.
ICE candidates represent possible connection paths. In this example, we use complete ICE gathering instead of trickle ICE for simplicity.

Step 4: Run Your Application

1

Start the Server

You should see:
2

Open in Browser

Navigate to http://localhost:8080 in your web browser.
3

Connect

Click the “Connect” button. You’ll see the connection establish in real-time.
4

Send Messages

Type a message and click “Send” or press Enter. Watch messages flow bidirectionally!

Expected Output

When you connect, the server terminal will show:
The browser will display:

More Complex Example: Pion-to-Pion

For server-to-server communication, check out this example from the Pion repository:
Find the complete pion-to-pion example with full signaling implementation at: github.com/pion/webrtc/tree/master/examples/pion-to-pion

Key Concepts Explained

PeerConnection Lifecycle

1

New

PeerConnection is created but no connection attempt started
2

Connecting

ICE candidates are being gathered and connectivity checks are running
3

Connected

At least one ICE candidate pair succeeded and media can flow
4

Failed

Connection couldn’t be established (may be recoverable with ICE restart)
5

Closed

Connection was explicitly closed
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

  • Verify STUN server is reachable
  • Check firewall settings
  • Ensure ICE candidates are being exchanged
  • Try adding a TURN server for relay
  • 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
  • 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
  • Use unordered, unreliable channels for real-time data
  • Consider adjusting MaxRetransmits
  • Check network conditions with stats
  • Monitor DataChannel.BufferedAmount
Production Checklist:
  • Implement proper error handling
  • Use secure signaling (WSS, HTTPS)
  • Add authentication/authorization
  • Configure TURN servers for NAT traversal
  • Monitor connection statistics
  • Handle reconnection logic
  • Clean up resources on close

Learn More

WebRTC for the Curious

Understand WebRTC protocols at a deep level: ICE, DTLS, SCTP, RTP, and more
Join the Community: Happy coding! 🚀