From d3042d501da99e671e4942f813c83da9b30d5688 Mon Sep 17 00:00:00 2001 From: Vinzent Date: Thu, 3 Oct 2024 18:14:07 +0200 Subject: [PATCH] feat: add logging to realtime_client --- .../lib/src/realtime_client.dart | 41 +++++++++++++------ packages/realtime_client/pubspec.yaml | 1 + .../lib/src/supabase_auth.dart | 2 + 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/realtime_client/lib/src/realtime_client.dart b/packages/realtime_client/lib/src/realtime_client.dart index fe7eefd3..4241173c 100644 --- a/packages/realtime_client/lib/src/realtime_client.dart +++ b/packages/realtime_client/lib/src/realtime_client.dart @@ -4,6 +4,7 @@ import 'dart:core'; import 'package:collection/collection.dart'; import 'package:http/http.dart'; +import 'package:logging/logging.dart'; import 'package:meta/meta.dart'; import 'package:realtime_client/realtime_client.dart'; import 'package:realtime_client/src/constants.dart'; @@ -62,6 +63,7 @@ class RealtimeClient { final Duration timeout; final WebSocketTransport transport; final Client? httpClient; + final _log = Logger('supabase.realtime'); int heartbeatIntervalMs = 30000; Timer? heartbeatTimer; @@ -90,18 +92,29 @@ class RealtimeClient { /// Initializes the Socket /// - /// `endPoint` The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) - /// `httpEndpoint` The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol) - /// `transport` The Websocket Transport, for example WebSocket. - /// `timeout` The default timeout in milliseconds to trigger push timeouts. - /// `params` The optional params to pass when connecting. - /// `headers` The optional headers to pass when connecting. - /// `heartbeatIntervalMs` The millisec interval to send a heartbeat message. - /// `logger` The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`$kind: $msg`, data) } - /// `encode` The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload)) - /// `decode` The function to decode incoming messages. Defaults to JSON: (payload, callback) => callback(JSON.parse(payload)) - /// `longpollerTimeout` The maximum timeout of a long poll AJAX request. Defaults to 20s (double the server long poll timer). - /// `reconnectAfterMs` The optional function that returns the millsec reconnect interval. Defaults to stepped backoff off. + /// [endPoint] The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol + /// + /// [transport] The Websocket Transport, for example WebSocket. + /// + /// [timeout] The default timeout in milliseconds to trigger push timeouts. + /// + /// [params] The optional params to pass when connecting. + /// + /// [headers] The optional headers to pass when connecting. + /// + /// [heartbeatIntervalMs] The millisec interval to send a heartbeat message. + /// + /// [logger] The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`$kind: $msg`, data) } + /// + /// [encode] The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload)) + /// + /// [decode] The function to decode incoming messages. Defaults to JSON: (payload, callback) => callback(JSON.parse(payload)) + /// + /// [longpollerTimeout] The maximum timeout of a long poll AJAX request. Defaults to 20s (double the server long poll timer). + /// + /// [reconnectAfterMs] The optional function that returns the millsec reconnect interval. Defaults to stepped backoff off. + /// + /// [logLevel] Specifies the log level for the connection on the server. RealtimeClient( String endPoint, { WebSocketTransport? transport, @@ -155,6 +168,7 @@ class RealtimeClient { } try { + log('transport', 'connecting to $endPointURL'); connState = SocketStates.connecting; conn = transport(endPointURL, headers); @@ -202,6 +216,7 @@ class RealtimeClient { if (conn != null) { final oldState = connState; connState = SocketStates.disconnecting; + log('transport', 'disconnecting', {'code': code, 'reason': reason}); // Connection cannot be closed while it's still connecting. Wait for connection to // be ready and then close it. @@ -218,6 +233,7 @@ class RealtimeClient { } connState = SocketStates.disconnected; reconnectTimer.reset(); + log('transport', 'disconnected'); } this.conn = null; @@ -247,6 +263,7 @@ class RealtimeClient { /// Logs the message. Override `this.logger` for specialized logging. void log([String? kind, String? msg, dynamic data]) { + _log.finer('$kind: $msg', data); logger?.call(kind, msg, data); } diff --git a/packages/realtime_client/pubspec.yaml b/packages/realtime_client/pubspec.yaml index 1138e910..33df781b 100644 --- a/packages/realtime_client/pubspec.yaml +++ b/packages/realtime_client/pubspec.yaml @@ -11,6 +11,7 @@ environment: dependencies: collection: ^1.15.0 http: '>=0.13.0 <2.0.0' + logging: ^1.2.0 meta: ^1.7.0 web_socket_channel: '>=2.3.0 <4.0.0' diff --git a/packages/supabase_flutter/lib/src/supabase_auth.dart b/packages/supabase_flutter/lib/src/supabase_auth.dart index a5964d94..fe61d42f 100644 --- a/packages/supabase_flutter/lib/src/supabase_auth.dart +++ b/packages/supabase_flutter/lib/src/supabase_auth.dart @@ -154,6 +154,7 @@ class SupabaseAuth with WidgetsBindingObserver { for (final channel in realtime.channels) { // ignore: invalid_use_of_internal_member if (channel.isJoined) { + // ignore: invalid_use_of_internal_member channel.forceRejoin(); } } @@ -180,6 +181,7 @@ class SupabaseAuth with WidgetsBindingObserver { // ignore: invalid_use_of_internal_member if (channel.isJoined) { + // ignore: invalid_use_of_internal_member channel.forceRejoin(); } }