Skip to content

Commit

Permalink
feat: Add installationId in LiveQuery connect (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfakourii authored Nov 11, 2023
1 parent f757dad commit c5f08f6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [6.3.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-6.2.0...dart-6.3.0) (2023-11-11)

### Features

* Add `installationId` in LiveQuery `connect` ([#976](https://github.com/parse-community/Parse-SDK-Flutter/pull/976))

## [6.2.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/dart-6.1.0...dart-6.2.0) (2023-10-18)

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/dart/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 = '6.2.0';
const String keySdkVersion = '6.3.0';
const String keyLibraryName = 'Flutter Parse SDK';

// End Points
Expand Down
26 changes: 22 additions & 4 deletions packages/dart/lib/src/network/parse_live_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Subscription<T extends ParseObject> {
'error'
];
Map<String, Function> eventCallbacks = <String, Function>{};

void on(LiveQueryEvent op, Function callback) {
eventCallbacks[_liveQueryEvent[op.index]] = callback;
}
Expand Down Expand Up @@ -140,8 +141,10 @@ class LiveQueryClient {
reconnectingController = LiveQueryReconnectingController(
() => reconnect(userInitialized: false), getClientEventStream, _debug);
}

static LiveQueryClient get instance => _getInstance();
static LiveQueryClient? _instance;

static LiveQueryClient _getInstance({bool? debug, bool? autoSendSessionId}) {
String? liveQueryURL = ParseCoreData().liveQueryURL;
if (liveQueryURL == null) {
Expand Down Expand Up @@ -174,13 +177,14 @@ class LiveQueryClient {
bool _connecting = false;
late StreamController<LiveQueryClientEvent> _clientEventStreamController;
late Stream<LiveQueryClientEvent> _clientEventStream;
StreamController<String>? chanelStream;
late LiveQueryReconnectingController reconnectingController;

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

Future<void> reconnect({bool userInitialized = false}) async {
await _connect(userInitialized: userInitialized);
_connectLiveQuery();
await _connectLiveQuery();
}

int readyState() {
Expand Down Expand Up @@ -286,6 +290,8 @@ class LiveQueryClient {
_channel = channel;
channel.stream.listen((dynamic message) {
_handleMessage(message);

chanelStream?.sink.add(message);
}, onDone: () {
_clientEventStreamController.sink
.add(LiveQueryClientEvent.disconnected);
Expand Down Expand Up @@ -315,7 +321,7 @@ class LiveQueryClient {
}
}

void _connectLiveQuery() {
Future<void> _connectLiveQuery() async {
WebSocketChannel? channel = _channel;
if (channel == null) {
return;
Expand All @@ -333,10 +339,22 @@ class LiveQueryClient {
connectMessage['sessionToken'] = sessionId;
}
}

String? clientKey = ParseCoreData().clientKey;
if (clientKey != null) {
connectMessage['clientKey'] = clientKey;
}

String? masterKey = ParseCoreData().masterKey;
if (clientKey != null) connectMessage['clientKey'] = clientKey;
if (masterKey != null) connectMessage['masterKey'] = masterKey;
if (masterKey != null) {
connectMessage['masterKey'] = masterKey;
}

String? parseInstallation =
(await ParseInstallation.currentInstallation()).installationId;
if (parseInstallation != null) {
connectMessage['installationId'] = parseInstallation;
}

if (_debug) {
print('$_printConstLiveQuery: ConnectMessage: $connectMessage');
Expand Down
2 changes: 1 addition & 1 deletion packages/dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: parse_server_sdk
description: The Dart SDK to connect to Parse Server. Build your apps faster with Parse Platform, the complete application stack.
version: 6.2.0
version: 6.3.0
homepage: https://parseplatform.org
repository: https://github.com/parse-community/Parse-SDK-Flutter
issue_tracker: https://github.com/parse-community/Parse-SDK-Flutter/issues
Expand Down
63 changes: 63 additions & 0 deletions packages/dart/test/src/network/parse_live_query_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:async';
import 'dart:convert';

import 'package:parse_server_sdk/parse_server_sdk.dart';
import 'package:test/test.dart';

import '../../test_utils.dart';

void main() {
setUpAll(() async {
// Create a fake server
final channel = spawnHybridCode(r'''
import 'dart:io';
import 'package:stream_channel/stream_channel.dart';
hybridMain(StreamChannel channel) async {
var server = await HttpServer.bind('localhost', 0);
server.transform(WebSocketTransformer()).listen((webSocket) {
webSocket.listen((request) {
webSocket.add(request);
});
});
channel.sink.add(server.port);
}
''');

// Get port server
int port = await channel.stream.first as int;
await initializeParse(liveQueryUrl: 'http://localhost:$port');
});

test('should exist installationId in connect LiveQuery', () async {
// arrange
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('Test'));

// Set installationId
ParseInstallation parseInstallation = ParseInstallation();
parseInstallation.set(keyInstallationId, "1234");
final String objectJson = json.encode(parseInstallation.toJson(full: true));
await ParseCoreData()
.getStore()
.setString(keyParseStoreInstallation, objectJson);

// Initialize LiveQuery
final LiveQuery liveQuery = LiveQuery();
liveQuery.client.chanelStream = StreamController<String>();

// act
await liveQuery.client.reconnect();
await liveQuery.client.subscribe(query);

// assert
liveQuery.client.chanelStream?.stream.listen((event) {
if (event.contains('connect')) {
expect(true, event.contains('1234'));
}
});

// 10 millisecond hold for stream
await Future.delayed(Duration(milliseconds: 10));
});
}
3 changes: 2 additions & 1 deletion packages/dart/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import 'package:test/test.dart';

const serverUrl = 'https://example.com';

Future<void> initializeParse() async {
Future<void> initializeParse({String? liveQueryUrl}) async {
await Parse().initialize(
'appId',
serverUrl,
liveQueryUrl: liveQueryUrl,
debug: true,
// to prevent automatic detection
fileDirectory: 'someDirectory',
Expand Down

0 comments on commit c5f08f6

Please sign in to comment.