HomeX-It / pusher-websocket-flutter

An unofficial Flutter plugin that wraps pusher-websocket-java on Android and pusher-websocket-swift on iOS
MIT License
30 stars 23 forks source link

An application doesn't receive data on iOS #14

Closed DorianSaboBM closed 4 years ago

DorianSaboBM commented 4 years ago

pusher_websocket_flutter: ^0.1.1

The code shown below is successfully initialized, connected and subscribed to pusher service but doesn't receive data from the channel.bind method. If I put logs in _initializePusherService and subscribe I can see them, but not from inside channel.bind.

Tested and does not work:

  1. Real devices with iOS versions: 12.4.3 and 13.1.3 (iPhone 5s and iPhone X)
  2. On every iPhone available in Xcode simulator

Note: This code works perfectly on any android version and device. (Receiving data)

Reproducible code


import 'dart:async';
import 'package:pusher_websocket_flutter/pusher.dart';

const channelName = "myChannelName";
const eventName = "myEventName";
const clusterName = "myClusterName";

class PusherService {
  static final PusherService _instance = PusherService._privateConstructor();

  StreamController<List> _streamController = StreamController<List>.broadcast();
  Sink get _inEventData => _streamController.sink;

  factory PusherService() {
    return _instance;
  }

  PusherService._privateConstructor() {
    _initializePusherService();
  }

  void _initializePusherService() async {
    await Pusher.init("pusherUrl", PusherOptions(cluster: clusterName));
    Pusher.connect(
        onConnectionStateChange: (ConnectionStateChange connectionState) async {
      print('PusherService - connection status changed: ' +
          connectionState.currentState);
    }, onError: (ConnectionError error) {
      print('PusherService - error: ' + error.message);
    });
  }

  void subscribe() {
    Pusher.subscribe(channelName).then((Channel channel) {
      channel.bind(eventName, (Event last) {
        print("PusherService - Received new data");
        _inEventData.add(last.data);
      });
    });
  }

  void unsubscribe() {
    print("PusherService - Unsubscribing from this service.");
    Pusher.unsubscribe(channelName);
  }
}

I am importing this class in main.dart fail and calling it like this

  @override
  void initState() {
    PusherService().subscribe();
  }

Here is my flutter doctor -v

flutter-doctor

MisterJimson commented 4 years ago

Could you be calling PusherService().subscribe(); too early?

_initializePusherService is async, so if you start the app and the initializing hasn't finished before you code in initstate happens you may run into issues. Perhaps on Android it happens fast enough that this issue doesn't happen.

DorianSaboBM commented 4 years ago

@MisterJimson Thanks for your answer. I don't think the problem is in sync because when I wrap my whole subscribe method in 5 seconds delay it still doesn't receive the data. If you (or someone else) have a working code for iOS I would be very grateful if you could share it here.

MisterJimson commented 4 years ago

There are 2 examples projects in this repo. Both work for me without issue on Android and iOS.

MisterJimson commented 4 years ago

Are you still having this issue? Did the examples work for you?