peer_rtc 1.0.0+2
peer_rtc: ^1.0.0+2 copied to clipboard
Simple peer-to-peer with WebRTC for Dart. PeerJS port for Flutter.
πΉοΈ 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 MeshHub for star-topology networks and Packer for optimized binary deltas (~80% bandwidth saving).
- Cross-Platform: Seamlessly runs on Mobile, Web, and Desktop.
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
- β MeshHub: Star topology P2P network with automatic host migration
- β Packer: Binary packing & delta compression (~80% bandwidth savings)
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