rtcnet

package module
v0.0.0-...-4fca6bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 17 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference

WebRTC Networking

This is my attempt at building and easy to use, client-server webrtc-based net.Conn implementation. Feel free to use it and file bug reports. I am by no means a webrtc expert, so if you see any problems with my implementation, then feel free to open an issue! Happy to answer any questions as needed!

Notes

  1. This is for client-server connections only! The main use case is if you want to use webrtc sockets in browser, but don't want to deal with the entire webrtc stack
  2. The connection is signaled over Websockets, so you don't need to use any ICE servers.

Platforms

I've tested this on:

  1. Browsers (Firefox, Chrome, Edge)
  2. Linux (desktop app)
  3. Windows (desktop app)

Things that I still need to do, but haven't yet

  • Replace logger with injectable logger interface
  • Ability for user to select the level of reliability/orderdness that they want on the data channel
  • Close websocket after webrtc negotiation has completed (currently ws stays open until net.Conn is closed)

Usage

import "github.com/unitoftime/rtcnet"

See Example

Deploying

All WebRTC traffic shares the listener's port number over UDP, alongside the websocket signaling on TCP. So only one port needs to be reachable, in both protocols.

Behind a 1:1 NAT (eg a docker bridge network, or a cloud VM without its public IP on an interface) set ListenConfig.PublicIP so clients are told the address they can actually reach. For example with docker compose:

ports:
  - "2000:2000/tcp"
  - "2000:2000/udp"
rtcnet.NewListener(":2000", rtcnet.ListenConfig{
	TlsConfig: tlsConfig,
	PublicIP: os.Getenv("PUBLIC_IP"),
	IceLite: true, // Optional, the listener is directly reachable so it only needs to answer connectivity checks
})

Note: Listener IceServers aren't needed in this setup. Their STUN lookups run on ephemeral ports, which aren't reachable when only the listen port is open. IceLite requires them to be empty.

Used By

  1. I'm currently using this for an online game I'm building for browser. You can find it here

License

  1. MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(newLogger zerolog.Logger)

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func Dial

func Dial(address string, tlsConfig *tls.Config, ordered bool, iceServers []string) (*Conn, error)

func DialContext

func DialContext(dialCtx context.Context, address string, tlsConfig *tls.Config, ordered bool, iceServers []string) (*Conn, error)

Dials until the data channel opens or dialCtx is done. The context only bounds the dial, the returned connection outlives it

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

type ListenConfig

type ListenConfig struct {
	TlsConfig      *tls.Config
	OriginPatterns []string
	// Note: STUN lookups run on ephemeral ports rather than the listen port, so their candidates are unreachable when only the listen port is open. Prefer PublicIP
	IceServers []string
	// The IPv4 address clients reach this listener at, advertised in place of the local interface addresses.
	// Needed behind a 1:1 NAT, such as a docker bridge network or a cloud VM without its public IP on an interface
	PublicIP string
	// Run ICE-lite: the listener only answers connectivity checks rather than also sending its own, which is less work and traffic per connection.
	// Only use it when clients can reach the listener directly at its advertised address, ie a public interface or PublicIP with the port forwarded. Lite only uses host candidates, so IceServers must be empty
	IceLite bool
	// Max connections that are negotiating or waiting to be accepted. Beyond this, new requests are rejected with a 503. Defaults to 256
	MaxPendingConns int
}

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

func NewListener

func NewListener(address string, config ListenConfig) (*Listener, error)

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

func (*Listener) Close

func (l *Listener) Close() error

Note: This also closes the shared UDP port, which ends every connection accepted from this listener

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL