Skip to content

Commit

Permalink
feat: add logging to realtime_client
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Oct 3, 2024
1 parent 729f29a commit d3042d5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
41 changes: 29 additions & 12 deletions packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -155,6 +168,7 @@ class RealtimeClient {
}

try {
log('transport', 'connecting to $endPointURL');
connState = SocketStates.connecting;
conn = transport(endPointURL, headers);

Expand Down Expand Up @@ -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.
Expand All @@ -218,6 +233,7 @@ class RealtimeClient {
}
connState = SocketStates.disconnected;
reconnectTimer.reset();
log('transport', 'disconnected');
}
this.conn = null;

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions packages/realtime_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 2 additions & 0 deletions packages/supabase_flutter/lib/src/supabase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down

0 comments on commit d3042d5

Please sign in to comment.