-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrealtime.dart
36 lines (28 loc) · 905 Bytes
/
realtime.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import 'dart:async';
import 'package:kuzzle/kuzzle.dart';
Future<void> main() async {
final kuzzle = Kuzzle(
WebSocketProtocol('127.0.0.1.xip.io'),
offlineMode: OfflineMode.auto,
);
final filters = <String, dynamic>{};
await kuzzle.connect();
final roomId =
await kuzzle.realtime.subscribe('index', 'collection', filters, listener);
print('unsubscribing to room $roomId');
Timer.periodic(Duration(seconds: 2), (_) {
print('publishing realtime message');
kuzzle.realtime.publish('index', 'collection', <String, dynamic>{
'message': 'hello world',
'sendedAt': DateTime.now().millisecondsSinceEpoch,
});
});
Timer(Duration(seconds: 60), () {
print('unsubscribing from room $roomId');
kuzzle.realtime.unsubscribe(roomId);
});
}
void listener(KuzzleResponse response) {
print('received realtime message');
print(response.result);
}