Skip to content

Commit

Permalink
feat and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
moshOntong-IT committed Jul 5, 2024
1 parent 1b798e9 commit adee5af
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
<br>

Expand All @@ -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<void> subscribe(String channel) async {
await realtime.subscribe([channel]);
Future<RealtimeSubscriptionExt> subscribe(String channel) async {
return await realtime.subscribe([channel]);
}
```

Expand Down Expand Up @@ -172,14 +175,25 @@ Future<void> doSomething() async{
}
```

### `reconnect`
## `reconnect`

You can manually reconnect to the server by calling the `reconnect` method.

```dart
realtime.reconnect();
```

<br>
<br>

## `setPingEnabled`

Whether to enable the ping pong mechanism. Default is `true`.

```dart
realtime.setPingEnabled(false);
```

---

## Contribute to Realtime Extension for Appwrite Flutter
Expand Down
7 changes: 7 additions & 0 deletions lib/src/services/src/realtime_browser_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -43,6 +45,11 @@ class RealtimeBrowserExt extends RealtimeBaseExt with RealtimeMixinExt {
@override
Future<void> reconnect() => toReconnect();

@override
void setPingEnabled({required bool enabled}) {
toSetPingEnabled(enabled: enabled);
}

@override
Stream<RealtimeState> get stateStream => stateController.stream;

Expand Down
8 changes: 8 additions & 0 deletions lib/src/services/src/realtime_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sealed class RealtimeExt {
required Client client,
int retryAttempts = 3,
int pingInterval = 30,
bool pingEnabled = true,
bool autoReconnect = true,
});

Expand All @@ -51,6 +52,9 @@ sealed class RealtimeExt {
/// Disposing the Realtime resources
Future<void> dispose();

/// Set the ping enabled
void setPingEnabled({required bool enabled});

/// Getting the state of the Realtime
Stream<RealtimeState> get stateStream;

Expand All @@ -76,6 +80,7 @@ abstract class RealtimeBaseExt implements RealtimeExt {
required Client client,
int retryAttempts = 3,
int pingInterval = 30,
bool pingEnabled = true,
bool autoReconnect = true,
});

Expand All @@ -85,6 +90,9 @@ abstract class RealtimeBaseExt implements RealtimeExt {
@override
Future<void> dispose();

@override
void setPingEnabled({required bool enabled});

@override
Stream<RealtimeState> get stateStream;
@override
Expand Down
7 changes: 7 additions & 0 deletions lib/src/services/src/realtime_io_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,6 +46,11 @@ class RealtimeIoExt extends RealtimeBaseExt with RealtimeMixinExt {
@override
Future<void> reconnect() => toReconnect();

@override
void setPingEnabled({required bool enabled}) {
toSetPingEnabled(enabled: enabled);
}

@override
Stream<RealtimeState> get stateStream => stateController.stream;

Expand Down
16 changes: 15 additions & 1 deletion lib/src/services/src/realtime_mixin_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -227,7 +230,9 @@ mixin RealtimeMixinExt {
},
);

_startPingTimer();
if (pingEnabled) {
_startPingTimer();
}
} on WebSocketException catch (e, stackTrace) {
debugPrint('Websocket: $e');
_setState(
Expand Down Expand Up @@ -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) {
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: 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
Expand Down

0 comments on commit adee5af

Please sign in to comment.