About Dart Faye
Dart Faye is a comprehensive implementation of the Faye publish-subscribe messaging system for Dart, fully compatible with the Bayeux protocol. Built with modern Dart practices, it provides a robust foundation for real-time communication in Dart and Flutter applications.
Whether you're building a chat application, real-time dashboard, or any system requiring pub/sub messaging, Dart Faye offers the performance, reliability, and ease of use you need.
Key Features
Bayeux Protocol Support
Full implementation of the Bayeux protocol for real-time messaging with complete specification compliance.
Multiple Transport Types
Support for HTTP long-polling, WebSocket, and callback-polling to adapt to any server environment.
Streaming API
Modern Dart streams for reactive programming with state, message, and error streams.
Cross-Platform
Works seamlessly on Dart VM, Flutter, and web platforms with consistent API.
Type Safety
Full type safety with Dart's strong typing system for reliable, maintainable code.
Error Handling
Robust error handling with detailed error types for network, authentication, and protocol errors.
Installation
Add the following to your pubspec.yaml
:
dependencies:
faye: ^1.0.0
Then run:
dart pub get
Quick Start
Basic Usage
import 'package:faye/faye.dart';
void main() async {
// Create a client
final client = Client('http://localhost:8000/bayeux');
// Connect to the server
await client.connect();
// Subscribe to a channel
final subscription = await client.subscribe('/chat/room1', (data) {
print('Received message: $data');
});
// Publish a message
await client.publish('/chat/room1', {
'user': 'Alice',
'message': 'Hello, world!'
});
// Unsubscribe when done
await client.unsubscribe('/chat/room1');
// Disconnect
await client.disconnect();
}
Advanced Usage
import 'package:faye/faye.dart';
void main() async {
// Create client with options
final client = Client('http://localhost:8000/bayeux', {
'timeout': 30,
'interval': 1000,
});
// Listen to connection state changes
client.stateStream.listen((state) {
switch (state) {
case Client.unconnected:
print('Disconnected');
break;
case Client.connecting:
print('Connecting...');
break;
case Client.connected:
print('Connected');
break;
case Client.disconnected:
print('Disconnected');
break;
}
});
// Listen to errors
client.errorStream.listen((error) {
print('Error: ${error.message}');
});
// Connect
await client.connect();
// Subscribe to multiple channels
final chatSubscription = await client.subscribe('/chat/**', (data) {
print('Chat message: $data');
});
final notificationSubscription = await client.subscribe('/notifications', (data) {
print('Notification: $data');
});
// Publish messages
await client.publish('/chat/room1', {'message': 'Hello from room 1'});
await client.publish('/chat/room2', {'message': 'Hello from room 2'});
await client.publish('/notifications', {'type': 'info', 'text': 'System update'});
// Get client statistics
final stats = client.statistics;
print('Client stats: $stats');
// Clean up
await client.close();
}
Transport Types
HTTP Long-Polling
Default transport that works with any HTTP server supporting the Bayeux protocol.
client.setTransport('http');
WebSocket
For better performance when the server supports WebSocket transport.
client.setTransport('websocket');
Callback-Polling
For environments where WebSocket is not available and JSONP is required.
client.setTransport('callback-polling');
Channel Patterns
The library supports channel patterns for subscribing to multiple channels:
/chat/*: Matches /chat/room1, /chat/room2, etc.
/chat/**: Matches /chat/room1, /chat/room1/messages, etc.
/users/*/status: Matches /users/alice/status, /users/bob/status, etc.
Error Handling
The library provides comprehensive error handling with specific error types:
client.errorStream.listen((error) {
if (error.isNetworkError) {
print('Network error: ${error.message}');
} else if (error.isAuthenticationError) {
print('Authentication error: ${error.message}');
} else if (error.isSubscriptionError) {
print('Subscription error: ${error.message}');
}
});
Error Types
FayeError.network()
: Network-related errorsFayeError.timeout()
: Timeout errorsFayeError.protocol()
: Protocol errorsFayeError.authentication()
: Authentication errorsFayeError.subscription()
: Subscription errorsFayeError.publication()
: Publication errorsFayeError.channel()
: Channel validation errors
Documentation & Examples
See the example/
directory for complete working examples:
basic_client.dart
: Basic client usagechat_app.dart
: Simple chat applicationmulti_transport.dart
: Using multiple transport typeserror_handling.dart
: Comprehensive error handling