pusher / pusher-channels-flutter

Pusher Channels client library for Flutter targeting IOS, Android, and WEB
MIT License
74 stars 133 forks source link

Cant able to listen events for private channels after autheization as well #112

Open RajatKhoware opened 1 year ago

RajatKhoware commented 1 year ago

This is the full code of pusher

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:pusher_channels_flutter/pusher-js/core/auth/options.dart';
import 'package:pusher_channels_flutter/pusher-js/core/auth/pusher_authorizer.dart';
import 'package:pusher_channels_flutter/pusher-js/core/options.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pusher_channels_flutter/pusher_channels_flutter.dart';

class PusherTest extends StatefulWidget {
  const PusherTest({Key? key}) : super(key: key);

  @override
  State<PusherTest> createState() => _PusherTestState();
}

class _PusherTestState extends State<PusherTest> {
  PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();
  String _log = 'output:\n';
  final apiKey = "";
  final cluster = "ap2";
  final channelName = "";
  final String eventName = "message-send";
  String token = "";
  final _eventFormKey = GlobalKey<FormState>();
  final _listViewController = ScrollController();
  final _data = TextEditingController();

  void log(String text) {
    print("LOG: $text");
    setState(
      () {
        _log += text + "\n";
        Timer(
          const Duration(milliseconds: 100),
          () {
            _listViewController
                .jumpTo(_listViewController.position.maxScrollExtent);
          },
        );
      },
    );
  }

  @override
  void initState() {
    super.initState();
    onConnectPressed();
  }

  void onConnectPressed() async {
    // Remove keyboard
    // FocusScope.of(context).requestFocus(FocusNode());

    try {
      await pusher.init(
        apiKey: apiKey,
        cluster: cluster,
        onConnectionStateChange: onConnectionStateChange,
        onError: onError,
        onSubscriptionSucceeded: onSubscriptionSucceeded,
        onEvent: onEvent,
        onSubscriptionError: onSubscriptionError,
        onDecryptionFailure: onDecryptionFailure,
        onMemberAdded: onMemberAdded,
        onMemberRemoved: onMemberRemoved,
        onSubscriptionCount: onSubscriptionCount,
        //authEndpoint: "https://meinhaus.ca/broadcasting/auth",
        onAuthorizer: onAuthorizer,
      );
      await pusher.subscribe(channelName: channelName);
      await pusher.connect();
    } catch (e) {
      log("ERROR: $e");
    }
  }

  void onConnectionStateChange(dynamic currentState, dynamic previousState) {
    log("Connection: $currentState");
  }

  void onError(String message, int? code, dynamic e) {
    log("onError: $message code: $code exception: $e");
  }

  void onEvent(PusherEvent event) {
    print("New event");
    log("onEvent: $event");
  }

  void onSubscriptionSucceeded(String channelName, dynamic data) {
    log("onSubscriptionSucceeded: $channelName data: $data");
    final me = pusher.getChannel(channelName)?.me;
    log("Me: $me");
  }

  void onSubscriptionError(String message, dynamic e) {
    log("onSubscriptionError: $message Exception: $e");
  }

  void onDecryptionFailure(String event, String reason) {
    log("onDecryptionFailure: $event reason: $reason");
  }

  void onMemberAdded(String channelName, PusherMember member) {
    log("onMemberAdded: $channelName user: $member");
  }

  void onMemberRemoved(String channelName, PusherMember member) {
    log("onMemberRemoved: $channelName user: $member");
  }

  void onSubscriptionCount(String channelName, int subscriptionCount) {
    log("onSubscriptionCount: $channelName subscriptionCount: $subscriptionCount");
  }

  dynamic onAuthorizer(
      String channelName, String socketId, dynamic options) async {
    var authUrl = "https://website.com/broadcasting/auth";
    var result = await http.post(
      Uri.parse(authUrl),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ${token}',
      },
      body: 'socket_id=' + socketId + '&channel_name=' + channelName + '',
    );
    var json = jsonDecode(result.body);
    print(json);
    print(json['auth']);
    return {
      "auth": json['auth'],
      //"channel_data": {"user_id": 2},
      "shared_secret": json["shared_secret"],
    };
  }

  void onTriggerEventPressed() async {
    if (_eventFormKey.currentState!.validate()) {
      pusher.trigger(
        PusherEvent(
          channelName: channelName,
          eventName: eventName,
          data: _data.text,
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            pusher.connectionState == 'DISCONNECTED'
                ? 'Pusher Channels Example'
                : channelName,
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView(
            controller: _listViewController,
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            children: <Widget>[
              if (pusher.connectionState != 'CONNECTED')
                ElevatedButton(
                  onPressed: onConnectPressed,
                  child: const Text('Connect'),
                )
              else
                Form(
                  key: _eventFormKey,
                  child: Column(
                    children: <Widget>[
                      ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: pusher.channels[channelName]?.members.length,
                        itemBuilder: (context, index) {
                          final member = pusher
                              .channels[channelName]!.members.values
                              .elementAt(index);

                          return ListTile(
                              title: Text(member.userInfo.toString()),
                              subtitle: Text(member.userId));
                        },
                      ),
                      TextFormField(
                        controller: _data,
                        decoration: const InputDecoration(
                          labelText: 'Data',
                        ),
                      ),
                      ElevatedButton(
                        onPressed: onTriggerEventPressed,
                        child: const Text('Trigger Event'),
                      ),
                    ],
                  ),
                ),
              SingleChildScrollView(child: Text(_log)),
            ],
          ),
        ),
      ),
    );
  }
}

when printing the response of onAuthorizer method we get this "auth": "278d425bdf160c739803:4708d583dada6a56435fb8bc611c77c359a31eebde13337c16ab43aa6de336ba",

benjamin-tang-pusher commented 1 year ago

Hi, are you getting an error message when the subscribe method is called? If it fails, the library should throw an error if your auth response isn't valid.

Also try going to your Pusher Debug console (you can get to your Pusher Debug Console by logging into your dashboard, navigating to your Channels app and clicking "Debug Console" on the left) to see if our server has processed the subscription from your test client. Does the log for this show?