GardenMobile & interfaces

An older npusher event and presence integration

A historical Flutter example for Echo-style events, authorization headers, and periodic presence updates.

A real-time integration can connect successfully and still interpret an event payload incorrectly. The original npusher example handled an Echo-style public event and periodic presence updates separately.

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'
          });
      _pusher.connect(
        (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) {
                _parsePresenceAndroid(event);
              } else if (Platform.isIOS) {
                _parsePresenceIOS(event);
              }
            };
            _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');
      initPusher(eventId);
    }
  }

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

The authorization values come from the surrounding application’s configuration and session, not literals included here. Connection, subscription authorization, and payload parsing are different checks.

The catch block immediately calls initialization again without a backoff or explicit retry boundary. Reconnection can also recreate subscriptions and timers, so the disposal method must match what was actually created. The platform-specific presence parsers are outside the excerpt.

This historical 1.0.3 integration was not built or connected for this revision.

← Back to the garden