This package is an unofficial pure dart implementation of Pusher Channels.
Author of the package currently needs help with testing the package on other OS platforms:
- Linux
- MacOS
Development and testing of:
- Presence channels
- Encrypted channels
Nicolas Britos - Pull requests #5, #6, #8
PusherChannelOptions was deprecated and renamed to PusherChannelsOptions for conveniency.
Note: This package needs to be tested and accepting issues. It was tested on a few projects for production.
This package is built according to the official documentation of Pusher Channels protocol.
The package has flexibale API containing interfaces for building custom connection and authorization. By default, it supports connection to Web sockets over the web_socket_channel out of the box.
This package was tested on:
- Anrdoid
- IOS
- Web
- Windows
Other platforms are in a test queue.
In order to get known to the parameters provided to PusherChannelsOptions, it's highly recommended to read the informative API reference of PusherChannelsOptions.
Also, see API references of the constructors to learn more about their parameters:
Create an instance of PusherChannelsClient and use it to establish connection.
- Create
PusherChannelsOptions
const options = PusherChannelsOptions.wss(
host: 'my.domain.com',
port: 443,
key: 'API_KEY',
protocol: 7);
- Create the client.
Use PusherChannelsClient.websocket constructor to create a client based on web sockets.
final client = PusherChannelsClient.websocket(
reconnectTries: 2,
options: options,
// Handle the errors based on the web sockets connection
onConnectionErrorHandle: (error, trace, refresh) {});- Create channels and listen for events.
// Ensure you implement your logic
// after successfull connection to your server
Channel? channel;
StreamSubscription? eventSubscription;
// This stream will recieve events and notify subscribers
// whenever the client is connected or reconnected after potential error
client.onConnectionEstablished.listen((_) async {
channel ??= client.publicChannel('my_public_channel_name');
await eventSubscription?.cancel();
// Ensure you bind to the channel before subscribing,
// otherwise - you will miss some events
eventSubscription = channel?.bind('my_event').listen((event) {
//listen for events form the channel here
});
channel!.subscribe();
});
client.connect();
// unsubscribe when done with channel
eventSubscription?.cancel();
channel?.unsubscribe();
//close when done with client
client.close();Note: For now, the package supports only reading (recieving) events. Triggering events is in the milestones.
The package supports 2 types of channels:
- Public
- Private
- Triggering events (for now plugin supprts only reading events from channels)
- Presence channels
- Encrypted channels
The package comes with the default delegate for authorizing to private channels over http.
final privateChannel ??= client.privateChannel(
'my_private_channel',
// This is a default authorization delegate
// to authorize to the channels
// You may implement your own authorization delegate
// implementing [AuthorizationDelegate] interface
// Use `http` or `https` scheme
TokenAuthorizationDelegate(
authorizationEndpoint: Uri.parse('http://my.auth.com/api/auth'),
headers: {'Authorization': 'Bearer [YOUR_TOKEN]'}));By default, logs are disabled.
PusherChannelsPackageConfigs.enableLogs();
//or
PusherChannelsPackageConfigs.disableLogs();