Skip to content

Live query connection stream #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 1.0.26

## 1.0.25
Update dependencies

## 1.0.24
Fixed lint
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Want to get involved? Join our Slack channel and help out! (http://flutter-parse
To install, either add to your pubspec.yaml
```yml
dependencies:
parse_server_sdk: ^1.0.25
parse_server_sdk: ^1.0.26
```
or clone this repository and add to your project. As this is an early development with multiple contributors, it is probably best to download/clone and keep updating as an when a new feature is added.

Expand Down Expand Up @@ -330,7 +330,7 @@ You’ll get the LiveQuery events through this subscription.
The first time you call subscribe, we’ll try to open the WebSocket connection to the LiveQuery server for you.

```dart
await liveQuery.subscribe(query);
Subscription subscription = await liveQuery.client.subscribe(query);
```

__Event Handling__
Expand All @@ -340,7 +340,7 @@ __Create event__
When a new ParseObject is created and it fulfills the QueryBuilder you subscribe, you’ll get this event.
The object is the ParseObject which was created.
```dart
liveQuery.on(LiveQueryEvent.create, (value) {
subscription.on(LiveQueryEvent.create, (value) {
print('*** CREATE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
Expand All @@ -356,7 +356,7 @@ When an existing ParseObject which fulfills the QueryBuilder you subscribe is up
QueryBuilder before and after changes), you’ll get this event.
The object is the ParseObject which was updated. Its content is the latest value of the ParseObject.
```dart
liveQuery.on(LiveQueryEvent.update, (value) {
subscription.on(LiveQueryEvent.update, (value) {
print('*** UPDATE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
Expand All @@ -372,7 +372,7 @@ When an existing ParseObject’s old value does not fulfill the QueryBuilder but
you’ll get this event. The object is the ParseObject which enters the QueryBuilder.
Its content is the latest value of the ParseObject.
```dart
liveQuery.on(LiveQueryEvent.enter, (value) {
subscription.on(LiveQueryEvent.enter, (value) {
print('*** ENTER ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
Expand All @@ -388,7 +388,7 @@ When an existing ParseObject’s old value fulfills the QueryBuilder but its new
you’ll get this event. The object is the ParseObject which leaves the QueryBuilder.
Its content is the latest value of the ParseObject.
```dart
liveQuery.on(LiveQueryEvent.leave, (value) {
subscription.on(LiveQueryEvent.leave, (value) {
print('*** LEAVE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
Expand All @@ -403,7 +403,7 @@ __Delete event__
When an existing ParseObject which fulfills the QueryBuilder is deleted, you’ll get this event.
The object is the ParseObject which is deleted
```dart
liveQuery.on(LiveQueryEvent.delete, (value) {
subscription.on(LiveQueryEvent.delete, (value) {
print('*** DELETE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
Expand All @@ -420,7 +420,7 @@ After that, you won’t get any events from the subscription object and will clo
LiveQuery server.

```dart
await liveQuery.unSubscribe();
liveQuery.client.unSubscribe(subscription);
```

## Users
Expand Down
2 changes: 1 addition & 1 deletion lib/parse_server_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Parse {

_hasBeenInitialized = true;

return Parse();
return this;
}

bool hasParseBeenInitialized() => _hasBeenInitialized;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of flutter_parse_sdk;

// Library
const String keySdkVersion = '1.0.25';
const String keySdkVersion = '1.0.26';
const String keyLibraryName = 'Flutter Parse SDK';

// End Points
Expand Down
21 changes: 20 additions & 1 deletion lib/src/network/parse_live_query.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:connectivity/connectivity.dart';
import 'package:flutter/widgets.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:connectivity/connectivity.dart';

import '../../parse_server_sdk.dart';

Expand All @@ -31,10 +32,16 @@ class Subscription {
}
}

enum LiveQueryClientEvent { CONNECTED, DISCONNECTED }

class Client with WidgetsBindingObserver {
factory Client() => _getInstance();
Client._internal(
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
_clientEventStreamController = StreamController<LiveQueryClientEvent>();
_clientEventStream =
_clientEventStreamController.stream.asBroadcastStream();

_client = client ??
ParseHTTPClient(
sendSessionId:
Expand Down Expand Up @@ -68,6 +75,10 @@ class Client with WidgetsBindingObserver {
return _instance;
}

Stream<LiveQueryClientEvent> get getClientEventStream {
return _clientEventStream;
}

WebSocket _webSocket;
ParseHTTPClient _client;
bool _debug;
Expand All @@ -76,6 +87,8 @@ class Client with WidgetsBindingObserver {
String _liveQueryURL;
bool _userDisconnected = false;
bool _connecting = false;
StreamController<LiveQueryClientEvent> _clientEventStreamController;
Stream<LiveQueryClientEvent> _clientEventStream;

final Map<int, Subscription> _requestSubScription = <int, Subscription>{};

Expand Down Expand Up @@ -185,13 +198,17 @@ class Client with WidgetsBindingObserver {
_channel.stream.listen((dynamic message) {
_handleMessage(message);
}, onDone: () {
_clientEventStreamController.sink
.add(LiveQueryClientEvent.DISCONNECTED);
if (!_userDisconnected) {
reconnect();
}
if (_debug) {
print('$_printConstLiveQuery: Done');
}
}, onError: (Object error) {
_clientEventStreamController.sink
.add(LiveQueryClientEvent.DISCONNECTED);
if (!_userDisconnected) {
reconnect();
}
Expand All @@ -203,6 +220,7 @@ class Client with WidgetsBindingObserver {
ParseApiRQ.liveQuery, _debug, 'IOWebSocketChannel'));
});
} on Exception catch (e) {
_clientEventStreamController.sink.add(LiveQueryClientEvent.DISCONNECTED);
if (_debug) {
print('$_printConstLiveQuery: Error: ${e.toString()}');
}
Expand Down Expand Up @@ -286,6 +304,7 @@ class Client with WidgetsBindingObserver {
_requestSubScription.values.toList().forEach((Subscription subcription) {
_subscribeLiveQuery(subcription);
});
_clientEventStreamController.sink.add(LiveQueryClientEvent.CONNECTED);
return;
}
if (actionData.containsKey('requestId')) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: parse_server_sdk
description: Flutter plugin for Parse Server, (https://parseplatform.org), (https://back4app.com)
version: 1.0.25
version: 1.0.26
homepage: https://github.com/phillwiggins/flutter_parse_sdk
author: PhillWiggins <[email protected]>

Expand Down