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

Simple peer-to-peer with WebRTC for Dart. PeerJS port for Flutter.

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 MeshHub for star-topology networks and 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
  • ✅ Compatible with PeerJS
  • ✅ MeshHub: Star topology P2P network with automatic host migration
  • ✅ 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,
);

MeshHub (P2P Network) #

Create star topology P2P networks with automatic host migration.

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 with typed extensions (no magic strings!)
mesh.onPeerJoined.listen((entry) => print('${entry.peerId} joined'));
mesh.onPeerLeft.listen((entry) => print('${entry.peerId} left'));
mesh.onData.listen((e) => print('From ${e['from']}: ${e['data']}'));
mesh.onBinaryPayload.listen((bytes) => handleBytes(bytes));
mesh.onHostChanged.listen((hostId) => print('New host: $hostId'));

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.

License #

Apache License 2.0 Š 2025 HauTV

4
likes
0
points
182
downloads

Publisher

verified publisherfreetalk.io.vn

Weekly Downloads

Simple peer-to-peer with WebRTC for Dart. PeerJS port for Flutter.

Homepage

Topics

#mesh #p2p #networking #peer #game

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