peer_rtc 1.1.0 copy "peer_rtc: ^1.1.0" to clipboard
peer_rtc: ^1.1.0 copied to clipboard

P2P WebRTC library for Dart/Flutter with mesh networking and binary optimization for real-time games.

Stark Logo

πŸ•ΉοΈ PeerRTC #

PeerRTC is the ultimate WebRTC solution for Dart & Flutter, designed to simplify real-time P2P networking. It solves the complexity of signaling, connection management, and data synchronization, making it effortless to build multiplayer games, collaborative tools, video calling apps, and secure file sharing systems.

πŸš€ Why PeerRTC?

  • Zero Server Cost: Direct P2P connects limit server load.
  • Game Ready: Built-in StarHub (star-topology) + MeshHub (full mesh) + Packer for optimized binary deltas (~80% bandwidth saving).
  • Cross-Platform: Seamlessly runs on Mobile, Web, and Desktop.

pub package License

Features #

  • βœ… Data channels (JSON & binary)
  • βœ… Media streams (audio/video)
  • βœ… Cross-platform (Android, iOS, Web, macOS, Windows, Linux)
  • βœ… Auto-reconnect with exponential backoff
  • βœ… Connection state tracking
  • βœ… PeerJS server compatible (API inspired by PeerJS, with extended features)
  • βœ… StarHub: Star topology P2P network with automatic host migration
  • βœ… MeshHub: Full mesh topology for small groups
  • βœ… MetaHub: Gossip mesh with room filtering for large-scale networks
  • βœ… Packer: Binary packing & delta compression (~80% bandwidth savings)

Note

Default free STUN servers cover ~80-90% of connections. For 100% reliability (bypassing symmetric NATs/Firewalls), you must configure your own TURN server.

Quick Start #

import 'package:peer_rtc/peer_rtc.dart';

final peer = Peer(
  options: PeerOptions(
    autoReconnect: true,
    debug: LogLevel.All,
  ),
);

peer.onOpen.listen((id) => print('Connected: $id'));

Data Connection #

// Connect
final conn = peer.connect('other-peer-id');
conn.onOpen.listen((_) => conn.send({'hello': 'world'}));

// Receive
peer.onConnection.listen((conn) {
  conn.onData.listen((data) => print('Got: $data'));
});

Media Call #

// Call
final stream = await navigator.mediaDevices.getUserMedia({'video': true});
final call = peer.call('other-peer-id', stream);
call.onStream.listen((s) => renderer.srcObject = s);

// Answer
peer.onCall.listen((call) async {
  final stream = await navigator.mediaDevices.getUserMedia({'video': true});
  call.answer(stream);
  call.onStream.listen((s) => renderer.srcObject = s);
});

Auto-Reconnect #

final peer = Peer(
  options: PeerOptions(
    autoReconnect: true,
    maxRetries: 5,
    baseRetryDelayMs: 1000,
  ),
);

peer.onReconnecting.listen((attempt) => print('Retry $attempt'));
peer.onReconnected.listen((_) => print('Reconnected!'));

Custom Server #

final peer = Peer.withServer(
  host: 'your-server.com',
  port: 9000,
  secure: true,
);

StarHub (Star Topology) #

Create star topology P2P networks with automatic host migration.

final star = StarHub(peer: peer);

// Host: create network
await star.create();
print('Join me: ${star.myPeerId}');

// Others: join
await star.join('host-peer-id');

// Broadcast to all
star.broadcast({'action': 'move', 'x': 100});

// Listen with typed extensions
star.onPeerJoined.listen((entry) => print('${entry.peerId} joined'));
star.onData.listen((e) => print('From ${e['from']}: ${e['data']}'));
star.onHostChanged.listen((hostId) => print('New host: $hostId'));

MeshHub (Full Mesh) #

Simple full mesh topology - every peer connects to every other peer.

final mesh = MeshHub(peer: peer, maxPeers: 10);

// Bootstrap with known peers
await mesh.bootstrap(['peer1', 'peer2']);

// Broadcast to all
mesh.broadcast({'action': 'move', 'x': 100});

// Listen for data
mesh.onPeerConnected.listen((id) => print('$id joined'));
mesh.onData.listen((e) => print('From ${e['from']}: ${e['payload']}'));

MetaHub (Gossip Mesh) #

Gossip-based mesh with room filtering for large-scale P2P networks.

final meta = MetaHub(peer: peer, k: 3);

// Bootstrap with known peers
await meta.bootstrap(['peer1', 'peer2']);

// Join a room (scoped messaging)
meta.joinRoom('game-123');

// Broadcast in room only
meta.broadcastRoomData({'action': 'move', 'x': 100});

// Broadcast globally
meta.broadcastGlobalData({'type': 'chat', 'msg': 'Hello!'});

// Listen for data
meta.onPeerConnected.listen((id) => print('$id connected'));
meta.onData.listen((e) => print('From ${e['from']}: ${e['payload']}'));

Hub Comparison #

Feature StarHub MeshHub MetaHub
Topology Star (1 host) Full mesh Gossip mesh
Scalability ~10-50 peers ~5-15 peers ~1000+ peers
Latency 2 hops max 1 hop (direct) Multi-hop
Host βœ… Auto-migration ❌ No host ❌ No host
Rooms ❌ No ❌ No βœ… Scoped messaging
Use Case Small games, chat Tiny groups (<15) Large lobbies, MMO

Packer (Binary Optimization) #

Optimized binary packing for real-time data - 75-82% bandwidth savings vs JSON.

// Define schema (can use toJson().keys for convenience)
Packer.schemas[0] = ['id', 'hp', 'x', 'y', 'isAlive'];

// Pack and send via MeshHub
Packer.accumulate(0, {'id': 1, 'hp': 100, 'x': 50.5, 'y': 30.2, 'isAlive': true});
mesh.broadcastBinary(Packer.ship());

// Receive and unpack
mesh.onBinaryPayload.listen((bytes) {
  final objects = Packer.unpackBatch(bytes);
  for (final obj in objects) {
    print('Player ${obj['id']} at (${obj['x']}, ${obj['y']})');
  }
});

See Mesh Documentation for Packer details, delta compression, and MeshHub integration.

Acknowledgments #

  • API design inspired by PeerJS - compatible with PeerJS signaling server
  • Built on flutter_webrtc
  • MeshHub and Packer are original features not found in PeerJS

License #

Apache License 2.0 Β© 2025 HauTV

4
likes
150
points
176
downloads
screenshot

Publisher

verified publisherfreetalk.io.vn

Weekly Downloads

P2P WebRTC library for Dart/Flutter with mesh networking and binary optimization for real-time games.

Homepage

Topics

#mesh #p2p #networking #peer #game

Documentation

API reference

Funding

Consider supporting this project:

paypal.me

License

unknown (license)

Dependencies

events_emitter, flutter, flutter_webrtc, http, web_socket_channel

More

Packages that depend on peer_rtc