Skip to content

Commit d3042d5

Browse files
committed
feat: add logging to realtime_client
1 parent 729f29a commit d3042d5

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

packages/realtime_client/lib/src/realtime_client.dart

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:core';
44

55
import 'package:collection/collection.dart';
66
import 'package:http/http.dart';
7+
import 'package:logging/logging.dart';
78
import 'package:meta/meta.dart';
89
import 'package:realtime_client/realtime_client.dart';
910
import 'package:realtime_client/src/constants.dart';
@@ -62,6 +63,7 @@ class RealtimeClient {
6263
final Duration timeout;
6364
final WebSocketTransport transport;
6465
final Client? httpClient;
66+
final _log = Logger('supabase.realtime');
6567
int heartbeatIntervalMs = 30000;
6668
Timer? heartbeatTimer;
6769

@@ -90,18 +92,29 @@ class RealtimeClient {
9092

9193
/// Initializes the Socket
9294
///
93-
/// `endPoint` The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
94-
/// `httpEndpoint` The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
95-
/// `transport` The Websocket Transport, for example WebSocket.
96-
/// `timeout` The default timeout in milliseconds to trigger push timeouts.
97-
/// `params` The optional params to pass when connecting.
98-
/// `headers` The optional headers to pass when connecting.
99-
/// `heartbeatIntervalMs` The millisec interval to send a heartbeat message.
100-
/// `logger` The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`$kind: $msg`, data) }
101-
/// `encode` The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
102-
/// `decode` The function to decode incoming messages. Defaults to JSON: (payload, callback) => callback(JSON.parse(payload))
103-
/// `longpollerTimeout` The maximum timeout of a long poll AJAX request. Defaults to 20s (double the server long poll timer).
104-
/// `reconnectAfterMs` The optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
95+
/// [endPoint] The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol
96+
///
97+
/// [transport] The Websocket Transport, for example WebSocket.
98+
///
99+
/// [timeout] The default timeout in milliseconds to trigger push timeouts.
100+
///
101+
/// [params] The optional params to pass when connecting.
102+
///
103+
/// [headers] The optional headers to pass when connecting.
104+
///
105+
/// [heartbeatIntervalMs] The millisec interval to send a heartbeat message.
106+
///
107+
/// [logger] The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`$kind: $msg`, data) }
108+
///
109+
/// [encode] The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
110+
///
111+
/// [decode] The function to decode incoming messages. Defaults to JSON: (payload, callback) => callback(JSON.parse(payload))
112+
///
113+
/// [longpollerTimeout] The maximum timeout of a long poll AJAX request. Defaults to 20s (double the server long poll timer).
114+
///
115+
/// [reconnectAfterMs] The optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
116+
///
117+
/// [logLevel] Specifies the log level for the connection on the server.
105118
RealtimeClient(
106119
String endPoint, {
107120
WebSocketTransport? transport,
@@ -155,6 +168,7 @@ class RealtimeClient {
155168
}
156169

157170
try {
171+
log('transport', 'connecting to $endPointURL');
158172
connState = SocketStates.connecting;
159173
conn = transport(endPointURL, headers);
160174

@@ -202,6 +216,7 @@ class RealtimeClient {
202216
if (conn != null) {
203217
final oldState = connState;
204218
connState = SocketStates.disconnecting;
219+
log('transport', 'disconnecting', {'code': code, 'reason': reason});
205220

206221
// Connection cannot be closed while it's still connecting. Wait for connection to
207222
// be ready and then close it.
@@ -218,6 +233,7 @@ class RealtimeClient {
218233
}
219234
connState = SocketStates.disconnected;
220235
reconnectTimer.reset();
236+
log('transport', 'disconnected');
221237
}
222238
this.conn = null;
223239

@@ -247,6 +263,7 @@ class RealtimeClient {
247263

248264
/// Logs the message. Override `this.logger` for specialized logging.
249265
void log([String? kind, String? msg, dynamic data]) {
266+
_log.finer('$kind: $msg', data);
250267
logger?.call(kind, msg, data);
251268
}
252269

packages/realtime_client/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ environment:
1111
dependencies:
1212
collection: ^1.15.0
1313
http: '>=0.13.0 <2.0.0'
14+
logging: ^1.2.0
1415
meta: ^1.7.0
1516
web_socket_channel: '>=2.3.0 <4.0.0'
1617

packages/supabase_flutter/lib/src/supabase_auth.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class SupabaseAuth with WidgetsBindingObserver {
154154
for (final channel in realtime.channels) {
155155
// ignore: invalid_use_of_internal_member
156156
if (channel.isJoined) {
157+
// ignore: invalid_use_of_internal_member
157158
channel.forceRejoin();
158159
}
159160
}
@@ -180,6 +181,7 @@ class SupabaseAuth with WidgetsBindingObserver {
180181

181182
// ignore: invalid_use_of_internal_member
182183
if (channel.isJoined) {
184+
// ignore: invalid_use_of_internal_member
183185
channel.forceRejoin();
184186
}
185187
}

0 commit comments

Comments
 (0)