peer_rtc 1.0.0
peer_rtc: ^1.0.0 copied to clipboard
Simple peer-to-peer with WebRTC for Dart. PeerJS port for Flutter.
PeerRTC #
Free peer-to-peer communication for Dart/Flutter using WebRTC.
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
- ✅ Compatible with PeerJS
Installation #
dependencies:
peer_rtc: ^1.0.0
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,
);
MeshHub (P2P Network) #
Create star topology P2P networks with automatic host migration.
import 'package:peer_rtc/src/mesh/mesh_hub.dart';
final mesh = MeshHub(peer: peer);
// Host: create network
await mesh.create();
print('Join me: ${mesh.myPeerId}');
// Others: join
await mesh.join('host-peer-id');
// Broadcast to all
mesh.broadcast({'action': 'move', 'x': 100});
// Listen for data
mesh.on<Map>('data').listen((e) => print(e));
Packer (Binary Optimization) #
Optimized binary packing for real-time data - 75-82% bandwidth savings vs JSON.
import 'package:peer_rtc/src/mesh/packer.dart';
// Define schema
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.on<Map>('binaryData').listen((event) {
List<Map> objects = Packer.unpackBatch(event['data']);
});
See Mesh Documentation for Packer details, delta compression, and MeshHub integration.
Links #
License #
Apache License 2.0 © 2025 HauTV