Skip to content
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

Fix realtime connection fields #503

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7829b1f
Added connection fields to android/ios platform constants
sacOO7 Feb 21, 2024
fb220ca
Added android specific method handlers for connection fields
sacOO7 Feb 21, 2024
553509e
Added missing ios specific constants to header file
sacOO7 Feb 21, 2024
2359a94
Added connection fields method handler implementation for ios
sacOO7 Feb 21, 2024
4484ef6
Added connection specific fields to platform constants dart
sacOO7 Feb 21, 2024
5362bff
Added dart methods to retrieve connection specific field values
sacOO7 Feb 21, 2024
5fd1f1f
Fixed indentation error in ablyflutter
sacOO7 Feb 21, 2024
3a109df
Fixed connection fields accessor issues for ios
sacOO7 Feb 21, 2024
5f40d84
Refactored connection dart file to include createRecoveryKey method
sacOO7 Feb 22, 2024
7609df0
Made connection field id and recoveryKey as sync instead of async
sacOO7 Feb 22, 2024
77a97e6
Refactored code as per review comments
sacOO7 Feb 22, 2024
62f4c80
removed connection error reason references from the code
sacOO7 Feb 22, 2024
59c820d
Added dart impl to handle notification from android when connection id
sacOO7 Feb 23, 2024
e697aa4
Updated android code to send connection id when connection state changes
sacOO7 Feb 23, 2024
5dddbe2
Added connectionIdUpdated constants to AblyPlatformConstants
sacOO7 Feb 23, 2024
09c3275
Added realtime callback whenever connection state changes, send conne…
sacOO7 Feb 23, 2024
25ddbd1
removed unnecessary fetchId and fetchKey method from the connection d…
sacOO7 Feb 23, 2024
e14238b
Removed unnecessary id setting from connection platformobject dart
sacOO7 Feb 23, 2024
e525c0e
Updated connectionId updated method to retun null in dart
sacOO7 Feb 23, 2024
032948f
refactored code, removed unnecessary additions
sacOO7 Feb 23, 2024
2e1839d
Fixed missing imports and build errors for android
sacOO7 Feb 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.realtime.Channel;
import io.ably.lib.realtime.CompletionListener;
import io.ably.lib.realtime.ConnectionStateListener;
import io.ably.lib.realtime.Presence;
import io.ably.lib.rest.AblyBase;
import io.ably.lib.rest.AblyRest;
Expand Down Expand Up @@ -112,6 +113,11 @@ public AblyMethodCallHandler(final MethodChannel methodChannel,
_map.put(PlatformConstants.PlatformMethod.restAuthGetClientId,
(methodCall, result) -> authMethodHandler.clientId(methodCall, result, AuthMethodHandler.Type.Rest));

// Connection specific handlers
_map.put(PlatformConstants.PlatformMethod.connectionId, this::connectionId);
_map.put(PlatformConstants.PlatformMethod.connectionKey, this::connectionKey);
_map.put(PlatformConstants.PlatformMethod.connectionRecoveryKey, this::connectionRecoveryKey);

// Push Notifications
_map.put(PlatformConstants.PlatformMethod.pushActivate, this::pushActivate);
_map.put(PlatformConstants.PlatformMethod.pushDeactivate, this::pushDeactivate);
Expand Down Expand Up @@ -451,7 +457,18 @@ public void notImplemented() {
return token[0];
};
}
result.success(clientHandle.createRealtime(clientOptions.options, applicationContext));

final long realtimeInstanceHandle = clientHandle.createRealtime(clientOptions.options, applicationContext);
final AblyRealtime realtime = instanceStore.getRealtime(realtimeInstanceHandle);
realtime.connection.on(new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange state) {
AblyFlutterMessage<String> channelMessage = new AblyFlutterMessage<>(realtime.connection.id, realtimeInstanceHandle);
methodChannel.invokeMethod(PlatformConstants.PlatformMethod.connectionIdUpdated, channelMessage);
}
});

result.success(realtimeInstanceHandle);
} catch (final AblyException e) {
handleAblyException(result, e);
}
Expand Down Expand Up @@ -559,6 +576,23 @@ private void restTime(@NonNull MethodCall methodCall, @NonNull MethodChannel.Res
time(result, instanceStore.getRest(ablyMessage.handle));
}

private void connectionId(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
final AblyFlutterMessage ablyMessage = (AblyFlutterMessage) methodCall.arguments;
AblyRealtime realtime = instanceStore.getRealtime(ablyMessage.handle);
result.success(realtime.connection.id);
}

private void connectionKey(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
final AblyFlutterMessage ablyMessage = (AblyFlutterMessage) methodCall.arguments;
AblyRealtime realtime = instanceStore.getRealtime(ablyMessage.handle);
result.success(realtime.connection.key);
}

private void connectionRecoveryKey(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
final AblyFlutterMessage ablyMessage = (AblyFlutterMessage) methodCall.arguments;
AblyRealtime realtime = instanceStore.getRealtime(ablyMessage.handle);
result.success(realtime.connection.recoveryKey);
}
private void time(@NonNull MethodChannel.Result result, AblyBase client) {
Callback<Long> callback = new Callback<Long>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ static final public class PlatformMethod {
public static final String realtimeAuthCreateTokenRequest = "realtimeAuthCreateTokenRequest";
public static final String realtimeAuthRequestToken = "realtimeAuthRequestToken";
public static final String realtimeAuthGetClientId = "realtimeAuthGetClientId";

public static final String connectionId = "connectionId";
public static final String connectionKey = "connectionKey";
public static final String connectionRecoveryKey = "connectionRecoveryKey";
public static final String connectionIdUpdated = "connectionIdUpdated";

public static final String pushActivate = "pushActivate";
public static final String pushDeactivate = "pushDeactivate";
public static final String pushReset = "pushReset";
Expand Down
36 changes: 35 additions & 1 deletion ios/Classes/AblyFlutter.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ -(void)reset;
ARTRealtime *const realtime = [[ARTRealtime alloc] initWithOptions:options.clientOptions];
[instanceStore setRealtime:realtime with:handle];

[realtime.connection on:^(ARTConnectionStateChange *stateChange) {
AblyFlutterMessage *const message =
[[AblyFlutterMessage alloc] initWithMessage:[realtime.connection id] handle: handle];
[ably.channel invokeMethod:AblyPlatformMethod_connectionIdUpdated arguments:message];
}];

// Giving Ably client the deviceToken registered at device launch (didRegisterForRemoteNotificationsWithDeviceToken).
// This is not an ideal solution. We save the deviceToken given in didRegisterForRemoteNotificationsWithDeviceToken and the
// error in didFailToRegisterForRemoteNotificationsWithError and pass it to Ably in the first client that is first created.
Expand Down Expand Up @@ -586,7 +592,7 @@ -(void)reset;

static const FlutterHandler _restTime = ^void(AblyFlutter *const ably, FlutterMethodCall *const call, const FlutterResult result) {
AblyFlutterMessage *const ablyMessage = call.arguments;

AblyInstanceStore *const instanceStore = [ably instanceStore];
ARTRest *const rest = [instanceStore restFrom:ablyMessage.handle];

Expand All @@ -599,6 +605,30 @@ -(void)reset;
}];
};

static const FlutterHandler _connectionId = ^void(AblyFlutter *const ably, FlutterMethodCall *const call, const FlutterResult result) {
AblyFlutterMessage *const ablyMessage = call.arguments;
AblyInstanceStore *const instanceStore = [ably instanceStore];
ARTRealtime *const realtime = [instanceStore realtimeFrom:ablyMessage.handle];
NSString *const connectionId = [realtime.connection id];
result(connectionId);
};

static const FlutterHandler _connectionKey = ^void(AblyFlutter *const ably, FlutterMethodCall *const call, const FlutterResult result) {
AblyFlutterMessage *const ablyMessage = call.arguments;
AblyInstanceStore *const instanceStore = [ably instanceStore];
ARTRealtime *const realtime = [instanceStore realtimeFrom:ablyMessage.handle];
NSString *const connectionKey = [realtime.connection key];
result(connectionKey);
};

static const FlutterHandler _connectionRecoveryKey = ^void(AblyFlutter *const ably, FlutterMethodCall *const call, const FlutterResult result) {
AblyFlutterMessage *const ablyMessage = call.arguments;
AblyInstanceStore *const instanceStore = [ably instanceStore];
ARTRealtime *const realtime = [instanceStore realtimeFrom:ablyMessage.handle];
NSString *const connectionRecoveryKey = [realtime.connection recoveryKey];
result(connectionRecoveryKey);
};

static const FlutterHandler _getNextPage = ^void(AblyFlutter *const ably, FlutterMethodCall *const call, const FlutterResult result) {
AblyFlutterMessage *const ablyMessage = call.arguments;

Expand Down Expand Up @@ -741,6 +771,10 @@ -(instancetype)initWithChannel:(FlutterMethodChannel *const)channel
AblyPlatformMethod_releaseRealtimeChannel: _releaseRealtimeChannel,
AblyPlatformMethod_realtimeTime:_realtimeTime,
AblyPlatformMethod_restTime:_restTime,
// Connection fields
AblyPlatformMethod_connectionId:_connectionId,
AblyPlatformMethod_connectionKey:_connectionKey,
AblyPlatformMethod_connectionRecoveryKey:_connectionRecoveryKey,
// Push Notifications
AblyPlatformMethod_pushActivate: PushHandlers.activate,
AblyPlatformMethod_pushRequestPermission: PushHandlers.requestPermission,
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/codec/AblyPlatformConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ extern NSString *const AblyPlatformMethod_realtimeAuthAuthorize;
extern NSString *const AblyPlatformMethod_realtimeAuthCreateTokenRequest;
extern NSString *const AblyPlatformMethod_realtimeAuthRequestToken;
extern NSString *const AblyPlatformMethod_realtimeAuthGetClientId;

extern NSString *const AblyPlatformMethod_connectionId;
extern NSString *const AblyPlatformMethod_connectionKey;
extern NSString *const AblyPlatformMethod_connectionRecoveryKey;
extern NSString *const AblyPlatformMethod_connectionIdUpdated;

extern NSString *const AblyPlatformMethod_pushActivate;
extern NSString *const AblyPlatformMethod_pushDeactivate;
extern NSString *const AblyPlatformMethod_pushReset;
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/codec/AblyPlatformConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
NSString *const AblyPlatformMethod_realtimeAuthCreateTokenRequest= @"realtimeAuthCreateTokenRequest";
NSString *const AblyPlatformMethod_realtimeAuthRequestToken= @"realtimeAuthRequestToken";
NSString *const AblyPlatformMethod_realtimeAuthGetClientId= @"realtimeAuthGetClientId";

NSString *const AblyPlatformMethod_connectionId = @"connectionId";
NSString *const AblyPlatformMethod_connectionKey = @"connectionKey";
NSString *const AblyPlatformMethod_connectionRecoveryKey = @"connectionRecoveryKey";
NSString *const AblyPlatformMethod_connectionIdUpdated = @"connectionIdUpdated";

NSString *const AblyPlatformMethod_pushActivate= @"pushActivate";
NSString *const AblyPlatformMethod_pushDeactivate= @"pushDeactivate";
NSString *const AblyPlatformMethod_pushReset= @"pushReset";
Expand Down
6 changes: 6 additions & 0 deletions lib/src/generated/platform_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class PlatformMethod {
'realtimeAuthCreateTokenRequest';
static const String realtimeAuthRequestToken = 'realtimeAuthRequestToken';
static const String realtimeAuthGetClientId = 'realtimeAuthGetClientId';

static const String connectionId = 'connectionId';
static const String connectionKey = 'connectionKey';
static const String connectionRecoveryKey = 'connectionRecoveryKey';
static const String connectionIdUpdated = 'connectionIdUpdated';

static const String pushActivate = 'pushActivate';
static const String pushDeactivate = 'pushDeactivate';
static const String pushReset = 'pushReset';
Expand Down
11 changes: 11 additions & 0 deletions lib/src/platform/src/method_call_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class AblyMethodCallHandler {
return onAuthCallback(call.arguments as AblyMessage);
case PlatformMethod.realtimeAuthCallback:
return onRealtimeAuthCallback(call.arguments as AblyMessage?);
case PlatformMethod.connectionIdUpdated:
return onConnectionIdUpdated(call.arguments as AblyMessage?);
case PlatformMethod.pushOnActivate:
return _onPushOnActivate(call.arguments as ErrorInfo?);
case PlatformMethod.pushOnDeactivate:
Expand Down Expand Up @@ -68,6 +70,15 @@ class AblyMethodCallHandler {
return realtime.options.authCallback!(tokenParams);
}

/// @nodoc
/// handles connection id updated for realtime instances
Future<Object?> onConnectionIdUpdated(AblyMessage<dynamic>? message) async {
final updatedConnId = message!.message as String;
final realtime = realtimeInstances[message.handle];
realtime?.connection.id = updatedConnId;
return null;
}

final PushActivationEventsInternal _pushActivationEvents =
Push.activationEvents as PushActivationEventsInternal;
final PushNotificationEventsInternal _pushNotificationEvents =
Expand Down
9 changes: 9 additions & 0 deletions lib/src/platform/src/realtime/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ class Connection extends PlatformObject {
///
/// See [connection state recover options](https://ably.com/docs/realtime/connection#connection-state-recover-options)
/// for more information.
@Deprecated('Use createRecoveryKey instead')
String? recoveryKey;

/// The createRecoveryKey returns key string can be used by another client to
/// recover this connection's state in the recover client options property.
///
/// See [connection state recover options](https://ably.com/docs/realtime/connection#connection-state-recover-options)
/// for more information.
Future<String?> createRecoveryKey() =>
invoke<String?>(PlatformMethod.connectionRecoveryKey);

/// The serial number of the last message to be received on this connection,
/// used automatically by the library when recovering or resuming a
/// connection.
Expand Down
Loading