diff --git a/CHANGELOG.md b/CHANGELOG.md index 466f5b5..d0f3402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.1.3+1 + +- feat: can now disable or enable the ping pong mechanism +- feat: fix and add documentation for `setPingEnabled` method and fix the documentation for `subscribe` method + # 0.1.2+1 - fix: Completer does not complete when the state is `Disconnected` diff --git a/README.md b/README.md index fbc00f2..63a5ce2 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,9 @@ The `initialize` method has the following parameters: - `client` (required): An instance of `Client` from Appwrite Flutter SDK. - `retryAttempts` (optional): The number of retry attempts to connect to the server. Default is 3. - `pingInterval` (optional): The interval between each retry attempt in milliseconds. Default is 30 seconds. + +- `pingEnabled` (optional): Whether to enable the ping pong mechanism. Default is `true`. + - `autoReconnect` (optional): Whether to automatically reconnect to the server when the connection is lost. Default is `true`. However, the `autoReconnect` will be stopped when it reaches the `retryAttempts`.
@@ -116,8 +119,8 @@ However, the Appwrite Websocket does not support two-way communication. So it is The subscribe method is similar to the Appwrite SDK. It subscribes to a channel and listens to the events. However, this time the method is asynchronous. ```dart -Future subscribe(String channel) async { - await realtime.subscribe([channel]); +Future subscribe(String channel) async { + return await realtime.subscribe([channel]); } ``` @@ -172,7 +175,7 @@ Future doSomething() async{ } ``` -### `reconnect` +## `reconnect` You can manually reconnect to the server by calling the `reconnect` method. @@ -180,6 +183,17 @@ You can manually reconnect to the server by calling the `reconnect` method. realtime.reconnect(); ``` +
+
+ +## `setPingEnabled` + +Whether to enable the ping pong mechanism. Default is `true`. + +```dart +realtime.setPingEnabled(false); +``` + --- ## Contribute to Realtime Extension for Appwrite Flutter diff --git a/lib/src/services/src/realtime_browser_ext.dart b/lib/src/services/src/realtime_browser_ext.dart index f56e0fd..105cf48 100644 --- a/lib/src/services/src/realtime_browser_ext.dart +++ b/lib/src/services/src/realtime_browser_ext.dart @@ -18,6 +18,7 @@ class RealtimeBrowserExt extends RealtimeBaseExt with RealtimeMixinExt { required Client client, int retryAttempts = 3, int pingInterval = 30, + bool pingEnabled = true, bool autoReconnect = true, }) async { this.client = client; @@ -27,6 +28,7 @@ class RealtimeBrowserExt extends RealtimeBaseExt with RealtimeMixinExt { isInitialized = true; isDisposed = false; + this.pingEnabled = pingEnabled; this.retryAttempts = retryAttempts; this.pingInterval = pingInterval; this.autoReconnect = autoReconnect; @@ -43,6 +45,11 @@ class RealtimeBrowserExt extends RealtimeBaseExt with RealtimeMixinExt { @override Future reconnect() => toReconnect(); + @override + void setPingEnabled({required bool enabled}) { + toSetPingEnabled(enabled: enabled); + } + @override Stream get stateStream => stateController.stream; diff --git a/lib/src/services/src/realtime_ext.dart b/lib/src/services/src/realtime_ext.dart index b780f15..09106f8 100644 --- a/lib/src/services/src/realtime_ext.dart +++ b/lib/src/services/src/realtime_ext.dart @@ -42,6 +42,7 @@ sealed class RealtimeExt { required Client client, int retryAttempts = 3, int pingInterval = 30, + bool pingEnabled = true, bool autoReconnect = true, }); @@ -51,6 +52,9 @@ sealed class RealtimeExt { /// Disposing the Realtime resources Future dispose(); + /// Set the ping enabled + void setPingEnabled({required bool enabled}); + /// Getting the state of the Realtime Stream get stateStream; @@ -76,6 +80,7 @@ abstract class RealtimeBaseExt implements RealtimeExt { required Client client, int retryAttempts = 3, int pingInterval = 30, + bool pingEnabled = true, bool autoReconnect = true, }); @@ -85,6 +90,9 @@ abstract class RealtimeBaseExt implements RealtimeExt { @override Future dispose(); + @override + void setPingEnabled({required bool enabled}); + @override Stream get stateStream; @override diff --git a/lib/src/services/src/realtime_io_ext.dart b/lib/src/services/src/realtime_io_ext.dart index d4886d4..143b8ca 100644 --- a/lib/src/services/src/realtime_io_ext.dart +++ b/lib/src/services/src/realtime_io_ext.dart @@ -20,6 +20,7 @@ class RealtimeIoExt extends RealtimeBaseExt with RealtimeMixinExt { required Client client, int retryAttempts = 3, int pingInterval = 30, + bool pingEnabled = true, bool autoReconnect = true, }) async { this.client = client; @@ -28,6 +29,7 @@ class RealtimeIoExt extends RealtimeBaseExt with RealtimeMixinExt { isInitialized = true; isDisposed = false; + this.pingEnabled = pingEnabled; this.retryAttempts = retryAttempts; this.pingInterval = pingInterval; this.autoReconnect = autoReconnect; @@ -44,6 +46,11 @@ class RealtimeIoExt extends RealtimeBaseExt with RealtimeMixinExt { @override Future reconnect() => toReconnect(); + @override + void setPingEnabled({required bool enabled}) { + toSetPingEnabled(enabled: enabled); + } + @override Stream get stateStream => stateController.stream; diff --git a/lib/src/services/src/realtime_mixin_ext.dart b/lib/src/services/src/realtime_mixin_ext.dart index 7fe6bb3..4a338e1 100644 --- a/lib/src/services/src/realtime_mixin_ext.dart +++ b/lib/src/services/src/realtime_mixin_ext.dart @@ -52,6 +52,9 @@ mixin RealtimeMixinExt { /// Indicates if the realtime should auto reconnect late final bool autoReconnect; + /// Indicated if the ping should be enable or not. + late final bool pingEnabled; + /// The state of the realtime RealtimeState state = const DisconnectedState(); @@ -227,7 +230,9 @@ mixin RealtimeMixinExt { }, ); - _startPingTimer(); + if (pingEnabled) { + _startPingTimer(); + } } on WebSocketException catch (e, stackTrace) { debugPrint('Websocket: $e'); _setState( @@ -457,6 +462,15 @@ mixin RealtimeMixinExt { // }); // } + void toSetPingEnabled({required bool enabled}) { + pingEnabled = enabled; + if (pingEnabled) { + _startPingTimer(); + } else { + _stopPingTimer(); + } + } + void _startPingTimer() { _pingTimer?.cancel(); // Cancel any existing timer _pingTimer = Timer.periodic(Duration(seconds: pingInterval), (timer) { diff --git a/pubspec.yaml b/pubspec.yaml index 91fbcae..0dfd936 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: app_realtime_ext description: A Realtime Clone from Appwrite Flutter sdk. The purpose of this is to extend the capability and separate from sdk-generator -version: 0.1.2+1 +version: 0.1.3+1 license: MIT homepage: https://github.com/moshOntong-IT/app_realtime_ext repository: https://github.com/moshOntong-IT/app_realtime_ext