initPusher method

Future initPusher()

Implementation

Future initPusher() async{

  try {

    var user = await StorebountyAuth.getUser();

    if (user == null) {
      return;
    }

    var userChatRoom = user['chat_room_id'];

    var initPusher = await pusher.init(
      logToConsole: true,
      apiKey: "9d9811c35339e56ab418",
      cluster: "eu",
      onAuthorizer: onAuthorizer,
      onConnectionStateChange: (value, value2) {
        print("connection status ${value2}");
      },
      onError: (value, value2, value3) {
        print("error ${value}");
      },
      onSubscriptionSucceeded: (value, value2) async {
        print("sub success ${value2}");
        // socketId = await pusher.getSocketId();
      },
      onEvent: (event) async {
        print(event);

        if (event.eventName == "pusher:subscription_succeeded") {
          return;
        }

        var res = jsonDecode(event.data)['message'];
        print(res);
        // var channelTypeCheck = res['channelType'];
        // if(channelTypeCheck == "b2b"){

        var data = {
          "id": res['id'],
          "ownMessage": res['ownMessage'],
          "sender": {
            "last_seen": res['sender']['last_seen'],
            "avatar_url": res['sender']['avatar_url'],
            "name": res['sender']['name'],
            "email": res['sender']['email']
          },
          "channelType": res['channelType'],
          "message": res['message'],
          "senderUserId": res['senderUserId'],
          "recipientUserId": res['recipientUserId'],
          "createdAt": convertStringDateToDateTime(res['createdAt'].toString()).toString(),
          "attachmentKey": res['attachmentKey'],
          "baseUrl": res['baseUrl'],
          "extraPayload": res['extraPayload'],
          "messageType": res['messageType'],
          "conversationId": res['conversationId'],
          "unreadCount": res['unreadCount'],
          "previewText": res['previewText'],
          "deviceId": res['deviceId']
        };


        // messages.insert(0, data);
        // notifyListeners();

      },
      onSubscriptionError: (value, value2) {
        print("sub result ${value2} ${value}");
      },
      onDecryptionFailure: (value, value2) {
        print("decrypt ${value2}");
      },
      onMemberAdded: (value, member) {},
      onMemberRemoved: (value, member) {},
    );

    // await pusher.;
    channel = await pusher.subscribe(
      channelName: "${userChatRoom}",
    );
    //
    await pusher.connect();

  } catch (e) {
    print("ERROR: $e");
  }

}