Flutter pusher with npusher

To deal with wrong data format response from backend with pusher via Laravel echo

This example works both android and ios


npusher: ^1.0.3


  /// Init pusher
  Future<void> initPusher(int eventId) async {
    try {
      await _pusher.init(
          appKey: Config.I.env.pusherKey,
          authUrl: '${Config.I.env.apiBaseUrl}/broadcasting/auth',
          headers: <String, String>{
            'Authorization': 'Bearer ${token.user.bearerToken}',
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        (String previousState, String currentState) async {
          if (currentState.toLowerCase() == 'connected') {
            final NChannel eventChannel =
                await _pusher.subscribe('event');
            await _pusher.bindEchoPublic(eventChannel, 'OrderCreated',
                (NEvent event) {
            // Remember dispose timer after close screen
            final String presenceChannelName = 'event-presence';
            final Function(NEvent event) onEventHere = (NEvent event) {
              logger.d('onEventHere: $event');
              if (Platform.isAndroid) {
              } else if (Platform.isIOS) {
            _pusherTimer = await _pusher.echoPresencePeriodicStart(
                presenceChannelName, onEventHere: onEventHere);
        (String message, String code, String exception) {
          logger.d('error: $message');
    } on PlatformException catch (e) {
      logger.d('initPusher Error: $e');

  /// Dispose pusher
  Future<void> _disposePusher() async {
    _pusherTimer = null;
    await _pusher.disconnect();

Leave a Reply

Your email address will not be published.Required fields are marked *