Skip to content

Commit

Permalink
fix(realtime_client): correct channel error data (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jul 24, 2023
1 parent f6854e1 commit 7fbd94c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/realtime_client/lib/src/realtime_channel.dart
Expand Up @@ -283,7 +283,7 @@ class RealtimeChannel {
}
return;
}).receive('timeout', (_) {
if (callback != null) callback('TIMED OUT');
if (callback != null) callback('TIMED_OUT');
return;
});
}
Expand Down
7 changes: 4 additions & 3 deletions packages/realtime_client/lib/src/realtime_client.dart
Expand Up @@ -136,7 +136,7 @@ class RealtimeClient {
if (connState != SocketStates.disconnected) {
connState = SocketStates.closed;
}
_onConnClose('');
_onConnClose();
},
);
} catch (e) {
Expand Down Expand Up @@ -369,13 +369,14 @@ class RealtimeClient {
}

/// communication has been closed
void _onConnClose(String event) {
void _onConnClose() {
final event = conn?.closeReason ?? '';
log('transport', 'close', event);

/// SocketStates.disconnected: by user with socket.disconnect()
/// SocketStates.closed: NOT by user, should try to reconnect
if (connState == SocketStates.closed) {
_triggerChanError();
_triggerChanError(event);
reconnectTimer.scheduleTimeout();
}
if (heartbeatTimer != null) heartbeatTimer!.cancel();
Expand Down
26 changes: 26 additions & 0 deletions packages/realtime_client/test/mock_test.dart
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:realtime_client/realtime_client.dart';
import 'package:realtime_client/src/constants.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -286,6 +287,31 @@ void main() {
]),
);
});

test("correct CHANNEL_ERROR data on heartbeat timeout", () async {
final subscribeCallback = expectAsync2((event, [data]) {
if (event == "CHANNEL_ERROR") {
expect(data, "heartbeat timeout");
} else {
expect(event, "CLOSED");
}
}, count: 2);

final channel = client.channel('public:todoos').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(
event: '*',
schema: 'public',
table: 'todos',
filter: 'id=eq.2'),
(_, [__]) {},
);

channel.subscribe(subscribeCallback);

await client.conn!.sink
.close(Constants.wsCloseNormal, "heartbeat timeout");
});
});

// Version of realtime prior to adding broadcast and presence.
Expand Down

0 comments on commit 7fbd94c

Please sign in to comment.