locality_core 1.0.9 copy "locality_core: ^1.0.9" to clipboard
locality_core: ^1.0.9 copied to clipboard

Realtime state synchronization across devices, powered by the Locality Social Cloud.

example/locality_core_example.dart

import 'dart:async';
import 'package:locality_core/locality_core.dart';
import 'package:locality_core/src/auth/exception/locality_authentication_exception.dart';
import 'package:m511chacha20/chacha20_key.dart';

void main() async {
  late LocalitySocialCloud cloud;
  try {
    cloud = await LocalitySocialCloud.startTest(
      userCredentials: UserCredentials("SocialCloudDev2", "123456"),
      developerCredentials: DeveloperCredentials(
        realmId: "ptr2",
        realmSecret: "h3w1uzXhmHc4JvZi6ZE6bGXlPPISH/t26Pb6vtGzffE=",
      ),
      isRegistration: false,
    );
  } on LocalityAuthenticationException {
    rethrow;
  } catch (unexpectedError) {
    rethrow;
  }

  AuthenticatedSessionUser authenticatedSessionUser = cloud.getLoggedInUser();

  ClickCounter clickCounter = ClickCounter();
  await LocalitySocialCloud.supervise(
    pubSub: clickCounter,
    key: ChaCha20Key.fromString("Hello World"),
  );

  GreenTick greenTick = GreenTick();
  Broadcast broadcast = clickCounter.increment(authenticatedSessionUser);
  broadcast.addObserver(greenTick);
  await greenTick.future;

  print('Current total clicks: ${clickCounter.count}');
}

class GreenTick implements BroadcastObserver {
  final Completer<void> _completer = Completer<void>();

  Future<void> get future => _completer.future;

  @override
  void onBroadcastChanged(Broadcast broadcast) {
    if (broadcast.status == BroadcastStatus.confirmed) {
      if (!_completer.isCompleted) {
        _completer.complete();
      }
    }
  }
}

class ClickCounter with PubSub {
  int count = 0;

  @override
  String getTopic() {
    return 'test:click-counter';
  }

  @override
  void onReceive(Broadcast localityEvent) {
    Map<String, dynamic> payload = localityEvent.getPayload();

    switch (localityEvent.event) {
      case "click":
        count += (payload["step"] as int? ?? 1);
        print('Click from user "${payload["userId"]}". Total count: $count');
        break;
      case "reset":
        count = 0;
        print('Counter reset by user "${payload["userId"]}".');
        break;
    }
  }

  Broadcast increment(AuthenticatedSessionUser sessionUser, {int step = 1}) {
    return send("click", {
      "userId": sessionUser.publicUser.id,
      "step": step,
    });
  }

  Broadcast reset(AuthenticatedSessionUser sessionUser) {
    return send("reset", {
      "userId": sessionUser.publicUser.id,
    });
  }
}
0
likes
150
points
369
downloads

Documentation

API reference

Publisher

verified publisherlocality.media

Weekly Downloads

Realtime state synchronization across devices, powered by the Locality Social Cloud.

Homepage
Repository

License

BSD-3-Clause (license)

Dependencies

archive, argon2_web, crypto, http, m511chacha20, phoenix_socket, uuid

More

Packages that depend on locality_core