flutter_peer 0.0.2 copy "flutter_peer: ^0.0.2" to clipboard
flutter_peer: ^0.0.2 copied to clipboard

A Flutter plugin that simplifies peer-to-peer communication using WebRTC, supporting signaling, media streams, and real-time events.

Flutter Peer #

A developer-friendly, lightweight, and reliable WebRTC plugin for Flutter, inspired by PeerJS. Establish direct peer-to-peer data, video, and audio connections with ease.

pub package License

🚀 Features #

  • Cross-Platform: Works out-of-the-box on Android, iOS, Web, and Desktop.
  • PeerJS Protocol: Fully compatible with existing peerjs-server instances.
  • Type-Safe Events: Uses Enums and dedicated callback methods (onOpen, onData, etc.) for a better developer experience.
  • Simplicity: No complex WebRTC negotiation (SDP/ICE) to manage; just use IDs to connect.
  • Built-in Media Controls: Easy methods to switch cameras, toggle audio/video, and manage speakerphone.
  • Standard-compliant: Uses standard WebRTC under the hood via flutter_webrtc.

📦 Installation #

Add flutter_peer to your pubspec.yaml:

dependencies:
  flutter_peer: ^0.0.1

🛠️ Platform Setup #

Android #

Add permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

iOS #

Add keys to your Info.plist:

<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) needs camera access for video calls.</string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) needs microphone access for audio calls.</string>

📖 Quick Start #

1. Initialize Peer #

By default, it connects to the public PeerJS cloud server.

import 'package:flutter_peer/flutter_peer.dart';

// Create a peer with a random ID
final peer = Peer();

peer.onOpen((id) {
  print('My peer ID is: $id');
});

2. Connect and Send Data #

Establish a DataConnection to another peer.

final conn = peer.connect('another-peer-id');

conn.onOpen(() {
  conn.send('Hello from Flutter!');
});

conn.onData((data) {
  print('Received data: $data');
});

3. Make and Receive Calls #

Establish a MediaConnection for audio/video.

// To make a call
final stream = await peer.getLocalStream();
final call = peer.call('another-peer-id', stream);

call.onStream((remoteStream) {
  // Use remoteStream in a RTCVideoRenderer
});

// To receive a call
peer.onCall((call) async {
  final localStream = await peer.getLocalStream();
  call.answer(localStream);
  
  call.onStream((remoteStream) {
    // Show remote video
  });
});

⚙️ Advanced Configuration #

Custom Signaling Server #

Host your own PeerServer for production apps.

final peer = Peer(
  id: 'my-custom-id',
  host: 'your-peer-server.com',
  port: 443,
  secure: true,
  path: '/myapp',
  key: 'peerjs',
);

Custom ICE (STUN/TURN) Servers #

Configure custom ICE servers for better NAT traversal.

final peer = Peer(
  config: IceConfiguration(
    iceServers: [
      IceServer(urls: ['stun:stun.l.google.com:19302']),
      IceServer(
        urls: ['turn:your-turn-server.com'],
        username: 'user',
        credential: 'password',
      ),
    ],
  ),
);

Media Controls #

flutter_peer provides high-level methods to control media streams:

await peer.switchCamera(); // Switch between front/back
await peer.turnOffCamera(off: true); // Toggle video
await peer.turnoffMicrophone(off: true); // Toggle audio
await peer.switchSpeakers(); // Toggle speakerphone

📱 Platform Support #

Platform Support
Android
iOS
Web
MacOS
Windows
Linux

📄 License #

This project is licensed under the Apache 2.0 License.

3
likes
140
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin that simplifies peer-to-peer communication using WebRTC, supporting signaling, media streams, and real-time events.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

eventify, flutter, flutter_web_plugins, flutter_webrtc, js, plugin_platform_interface, uuid, web, web_socket_channel

More

Packages that depend on flutter_peer

Packages that implement flutter_peer