Install the SDK

To install the SDK, add the following dependency to your pubspec.yaml file:

dependencies:
  pusher_channels_flutter: ^2.0.0

Initialize the SDK

import 'package:pusher_channels_flutter/pusher_channels_flutter.dart';

PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();

void initPusher() {
  pusher.init(
    apiKey: 'YOUR_APP_KEY',
    cluster: 'pulses',
    wsHost: 'ws.pulses.cloud',
  );

  pusher.connect();
}

Subscribe to a channel

pusher.subscribe(channelName: 'channel-name');

Bind to an event

pusher.bind(channelName: 'channel-name', eventName: 'event-name', onEvent: (last, event) {
  print('Received event: $event');
});

Publish an event from the server

To publish an event from the server, you can use the trigger method. Run the following command:

// First, run 'npm install pusher'
const Pusher = require('pusher');

const pusher = new Pusher('YOUR_APP_KEY', {
    wsHost: 'ws.pulses.cloud',
    encrypted: true,
    disableStats: true,
    enabledTransports: ['ws', 'wss'],
    cluster: 'pulses'
});

pusher.trigger('channel-name', 'event-name', {
    message: 'Hello World'
});

If there isn’t an example in a language that you are familiar with then refer to Pusher’s Channels server libraries page to check if anyone has created one in your language.

Unsubscribe from a channel

pusher.unsubscribe(channelName: 'channel-name');

Disconnect from Pusher

pusher.disconnect();