-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add basic topics example and readmes (#45)
* chore: add basic topics example and readmes * dart format * use switch expression instead of switch statement * update link Co-authored-by: Matt Straathof <11823378+bruuuuuuuce@users.noreply.github.com> * update link Co-authored-by: Matt Straathof <11823378+bruuuuuuuce@users.noreply.github.com> * update link Co-authored-by: Matt Straathof <11823378+bruuuuuuuce@users.noreply.github.com> * fix example topics config name --------- Co-authored-by: Matt Straathof <11823378+bruuuuuuuce@users.noreply.github.com>
- Loading branch information
1 parent
a71ed4d
commit e719284
Showing
6 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Running the Examples | ||
|
||
For each example, you will need to provide a Momento API key, which you can generate from the [Momento Web Console](https://console.gomomento.com/api-keys). | ||
|
||
Each subfolder has more specific instructions, but you can run examples in Dart files using `dart run <filename>` and start example Flutter apps using `flutter run -d <device_id>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Topics Examples | ||
|
||
## Prerequisites | ||
|
||
- [Installed Dart](https://dart.dev/get-dart) | ||
- Momento API key, which you can generate from the [Momento Web Console](https://console.gomomento.com/api-keys) | ||
|
||
## Basic Example | ||
|
||
A very basic example of a Topics subscriber can be run using: | ||
|
||
``` | ||
MOMENTO_API_KEY="your-api-key" dart run basic_subscriber.dart | ||
``` | ||
|
||
A very basic example of a Topics publisher can be run using: | ||
|
||
``` | ||
MOMENTO_API_KEY="your-api-key" dart run basic_publisher.dart | ||
``` | ||
|
||
## Advanced Example | ||
|
||
The advanced example shows how you can unsubscribe from a topic (i.e. cancel a stream of messages) as well as how you can enable logging at a desired logging level. | ||
|
||
``` | ||
MOMENTO_API_KEY="your-api-key" dart run advanced.dart | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:io'; | ||
import 'package:client_sdk_dart/client_sdk_dart.dart'; | ||
|
||
void main() async { | ||
var topicClient = TopicClient( | ||
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"), | ||
MobileTopicConfiguration.latest()); | ||
|
||
// publish 10 messages spaced 1 second apart | ||
for (final i in Iterable.generate(10)) { | ||
var result = | ||
await topicClient.publish("cache", "topic", StringValue("hi $i")); | ||
switch (result) { | ||
case TopicPublishSuccess(): | ||
print("Successful publish!"); | ||
case TopicPublishError(): | ||
print("Publish error: ${result.errorCode} ${result.message}"); | ||
} | ||
sleep(Duration(seconds: 1)); | ||
} | ||
|
||
topicClient.close(); | ||
print("Closed Momento Topics publisher"); | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
import 'package:client_sdk_dart/client_sdk_dart.dart'; | ||
|
||
void main() async { | ||
var topicClient = TopicClient( | ||
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"), | ||
MobileTopicConfiguration.latest()); | ||
|
||
var subscription = topicClient.subscribe("cache", "topic"); | ||
var messageStream = switch (subscription) { | ||
TopicSubscription() => subscription.stream, | ||
TopicSubscribeError() => throw Exception( | ||
"Subscribe error: ${subscription.errorCode} ${subscription.message}"), | ||
}; | ||
|
||
// cancel subscription 15 seconds from now | ||
Timer(const Duration(seconds: 15), () { | ||
print("Cancelling subscription!"); | ||
subscription.unsubscribe(); | ||
}); | ||
|
||
try { | ||
await for (final msg in messageStream) { | ||
switch (msg) { | ||
case TopicSubscriptionItemBinary(): | ||
print("Binary value: ${msg.value}"); | ||
case TopicSubscriptionItemText(): | ||
print("String value: ${msg.value}"); | ||
case TopicSubscriptionItemError(): | ||
print("Error receiving message: ${msg.errorCode}"); | ||
} | ||
} | ||
} catch (e) { | ||
print("Runtime type: ${e.runtimeType}"); | ||
print("Error with await for loop: $e"); | ||
} | ||
|
||
topicClient.close(); | ||
print("Closed Momento Topics subscriber"); | ||
exit(0); | ||
} |