Skip to content

Commit 7e79ccc

Browse files
author
Ephraim Nartey
committed
fix: reconnect websocket on error and disconnection
1 parent 6e85c71 commit 7e79ccc

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/data/chatwoot_repository.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class ChatwootRepositoryImpl extends ChatwootRepository {
5454
bool _isListeningForEvents = false;
5555
Timer? _publishPresenceTimer;
5656
Timer? _presenceResetTimer;
57+
Timer? _checkPingTimer;
58+
DateTime? _lastPingTime;
5759

5860
ChatwootRepositoryImpl(
5961
{required ChatwootClientService clientService,
@@ -153,11 +155,16 @@ class ChatwootRepositoryImpl extends ChatwootRepository {
153155
clientService.startWebSocketConnection(
154156
localStorage.contactDao.getContact()!.pubsubToken ?? "");
155157

156-
final newSubscription = clientService.connection!.stream.listen((event) async{
158+
final newSubscription = clientService.connection!.stream.listen(
159+
(event) async{
157160
ChatwootEvent chatwootEvent = ChatwootEvent.fromJson(jsonDecode(event));
158161
if (chatwootEvent.type == ChatwootEventType.welcome) {
159162
callbacks.onWelcome?.call();
160163
} else if (chatwootEvent.type == ChatwootEventType.ping) {
164+
if(_lastPingTime == null){
165+
_startPingCheck();
166+
}
167+
_lastPingTime = DateTime.now();
161168
callbacks.onPing?.call();
162169
} else if (chatwootEvent.type == ChatwootEventType.confirm_subscription) {
163170
if (!_isListeningForEvents) {
@@ -219,6 +226,11 @@ class ChatwootRepositoryImpl extends ChatwootRepository {
219226
} else {
220227
print("chatwoot unknown event: $event");
221228
}
229+
},
230+
onError: (e){
231+
//auto reconnect on error
232+
print("chatwoot websocket: $e");
233+
listenForEvents();
222234
});
223235
_subscriptions.add(newSubscription);
224236
}
@@ -236,6 +248,7 @@ class ChatwootRepositoryImpl extends ChatwootRepository {
236248
callbacks = ChatwootCallbacks();
237249
_presenceResetTimer?.cancel();
238250
_publishPresenceTimer?.cancel();
251+
_checkPingTimer?.cancel();
239252
_subscriptions.forEach((subs) {
240253
subs.cancel();
241254
});
@@ -248,6 +261,23 @@ class ChatwootRepositoryImpl extends ChatwootRepository {
248261
localStorage.contactDao.getContact()!.pubsubToken ?? "", action);
249262
}
250263

264+
/// Start a timer to check for missed pings
265+
void _startPingCheck() {
266+
_checkPingTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
267+
final now = DateTime.now();
268+
if (_lastPingTime != null &&
269+
now.difference(_lastPingTime!).inSeconds > 30) {
270+
print("No ping received in 30 seconds. Reconnecting...");
271+
_lastPingTime = null;
272+
_checkPingTimer?.cancel();
273+
_checkPingTimer = null;
274+
listenForEvents();
275+
//get any lost messages
276+
getMessages();
277+
}
278+
});
279+
}
280+
251281
///Publishes presence update to websocket channel at a 30 second interval
252282
void _publishPresenceUpdates() {
253283
sendAction(ChatwootActionType.update_presence);

0 commit comments

Comments
 (0)