nito_reverb 1.0.0
nito_reverb: ^1.0.0 copied to clipboard
A professional, robust, and lightweight Flutter package for seamless communication with Laravel Reverb.
import 'package:nito_reverb/nito_reverb.dart';
import 'package:logging/logging.dart';
// Example User Model for Type-Safe testing
class UserProfile {
final int id;
final String name;
UserProfile({required this.id, required this.name});
factory UserProfile.fromJson(Map<String, dynamic> json) {
return UserProfile(id: json['id'] as int, name: json['name'] as String);
}
@override
String toString() => 'UserProfile(id: $id, name: $name)';
}
final _logger = Logger('Example');
void _log(Object message) {
_logger.info(message);
}
void main() async {
// 1. Setup Logging
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((r) => _log('[${r.level.name}] ${r.message}'));
// 2. Configuration
final config = NitoReverbConfig(
host: 'reverb.example.com',
key: 'your-app-key',
port: 443,
encrypted: true,
);
// 3. Authentication (Optional - needed for Private/Presence)
final auth = HttpNitoReverbAuth(
url: 'https://api.example.com/broadcasting/auth',
headers: {
'Authorization': 'Bearer YOUR_TOKEN',
'Accept': 'application/json',
},
);
// 4. Initialize Client
final client = NitoReverbClient(config: config, auth: auth);
// 5. Monitor Connection State
client.stateStream.listen((state) {
_log('🔌 Connection Status: $state');
});
// 6. Connect
await client.connect();
// -------------------------------------------------------------------
// EXAMPLE: Public Channel
// -------------------------------------------------------------------
final chat = client.public('chat-room');
// Listen for raw events
chat.allEvents.listen((e) => _log('Raw Event: ${e.name} Data: ${e.data}'));
// -------------------------------------------------------------------
// EXAMPLE: Type-Safe Event Stream
// -------------------------------------------------------------------
chat.on<UserProfile>('UserJoined', UserProfile.fromJson).listen((user) {
_log('✨ Type-Safe User Joined: ${user.name} (ID: ${user.id})');
});
// -------------------------------------------------------------------
// EXAMPLE: Private Channel & Whispering
// -------------------------------------------------------------------
final orders = client.private('orders.1');
orders.dataOn('OrderUpdated').listen((data) {
_log('🛒 Order Status: ${data['status']}');
});
// Send a client-side "whisper" event (e.g., typing indicator)
orders.whisper('typing', {'is_typing': true});
// -------------------------------------------------------------------
// EXAMPLE: Presence Channel (Member Tracking)
// -------------------------------------------------------------------
final presence = client.presence('team-chat');
presence.membersStream.listen((members) {
_log('👥 Online Members Count: ${members.length}');
members.forEach((id, info) => _log(' - User $id: ${info['name']}'));
});
// -------------------------------------------------------------------
// EXAMPLE: Cleanup
// -------------------------------------------------------------------
// When done:
// client.dispose();
}