locality_social_cloud 1.6.0
locality_social_cloud: ^1.6.0 copied to clipboard
Write social apps with Flutter. This code connects to the backend of Locality Social Cloud to provide realtime state synchronization across devices.
example/locality_social_cloud_example.dart
import 'dart:async';
import 'dart:math' as math;
import 'package:locality_social_cloud/api/locality_social_cloud.dart';
import 'package:locality_social_cloud/api/candy/throttled_change_notifier.dart';
import 'package:uuid/uuid.dart';
class GlobalClickCounter extends ThrottledChangeNotifier with PubSub {
int counter = 0;
/// This is a global click counter. All users can increase the click counter. All users see the same click counter.
@override
String getTopic() {
return 'global-click-counter';
}
void increaseCounterByOne() {
send('increaseCounter', {'amount': 1});
}
@override
void onReceive(LocalityEvent localityEvent) {
switch (localityEvent.event) {
case 'increaseCounter':
counter += localityEvent.payload['amount'] as int;
notifyListeners();
break;
}
}
}
void example({required bool testMode}) async {
if (testMode) {
LocalitySocialCloud.startInTestMode();
} else {
// REQUIRES YOU TO REGISTER AT https://locality.media AS A DEVELOPER
await LocalitySocialCloud.up(
appId: "YOUR_APP_ID",
appSecret: "YOUR_APP_SECRET",
username: "YOUR_USERNAME",
password: "YOUR_PASSWORD");
}
GlobalClickCounter exampleController = GlobalClickCounter();
exampleController.addListener(() {
print("Click counter is at ${exampleController.counter}");
});
LocalitySocialCloud.supervise(exampleController);
for (int i = 0; i < 30; i++) {
exampleController.increaseCounterByOne();
}
exampleController.timeline.whenSynchronized(() {
print("NOW SYNCHRONIZED");
print("CLICK COUNTER: ${exampleController.counter}");
});
await Future.delayed(Duration(milliseconds: 3000));
DiscoverUsers discoverUsers = LocalitySocialCloud.discoverUsers();
discoverUsers.addListener(() {
print("WE DISCOVERED SOME USERS!");
for (var user in discoverUsers.discoveredUsers) {
print("WE DISCOVERED: ${user.id}");
}
});
discoverUsers.startingWith('Maltii');
}
void main() async {
example(testMode: true);
}
void geoChannelExample() {
GeoChannel geoIndex = GeoChannel('nearby-people', metadata: "Hello!!");
geoIndex.connect(PubSubSupervisor.supervisor);
Timer.periodic(Duration(milliseconds: 1000), (timer) {
double randomLatitude = 90.7128 + math.Random().nextDouble() * 10;
double randomLongitude = -74.0060 + math.Random().nextDouble() * 10;
geoIndex.putGeoEntity(Uuid().v1(), {},
latitude: randomLatitude,
longitude: randomLongitude,
lifetimeSeconds: 30);
geoIndex.getNearbyEntities(90.7128, -74.0060, 6).then((value) {
print(value.toString());
});
});
}