Skip to content

feat: Use cupertino http and websocket implementation #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/supabase/lib/src/realtime_client_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,29 @@ class RealtimeClientOptions {
/// the timeout to trigger push timeouts
final Duration? timeout;

/// The WebSocket implementation to use
final WebSocketTransport? webSocketTransport;

/// {@macro realtime_client_options}
const RealtimeClientOptions({
this.eventsPerSecond,
this.logLevel,
this.timeout,
this.webSocketTransport,
});

RealtimeClientOptions copyWith({
int? eventsPerSecond,
RealtimeLogLevel? logLevel,
Duration? timeout,
WebSocketTransport? webSocketTransport,
}) {
return RealtimeClientOptions(
// ignore: deprecated_member_use_from_same_package
eventsPerSecond: eventsPerSecond ?? this.eventsPerSecond,
logLevel: logLevel ?? this.logLevel,
timeout: timeout ?? this.timeout,
webSocketTransport: webSocketTransport ?? this.webSocketTransport,
);
}
}
1 change: 1 addition & 0 deletions packages/supabase/lib/src/supabase_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class SupabaseClient {
'apikey': _supabaseKey,
},
headers: {'apikey': _supabaseKey, ...headers},
transport: options.webSocketTransport,
logLevel: options.logLevel,
httpClient: _authHttpClient,
timeout: options.timeout ?? RealtimeConstants.defaultTimeout,
Expand Down
33 changes: 33 additions & 0 deletions packages/supabase_flutter/lib/src/platform_http_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:io';

import 'package:cupertino_http/cupertino_http.dart';
import 'package:http/http.dart' as http;
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:web_socket_channel/adapter_web_socket_channel.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

/// For iOS and macOS this returns a `CupertinoClient` and [http.Client] for the
/// rest of the platforms.
///
/// This is used to make HTTP requests use the platform's native HTTP client.
http.Client getPlatformHttpClient() {
if (Platform.isIOS || Platform.isMacOS) {
return CupertinoClient.defaultSessionConfiguration();
} else {
return http.Client();
}
}

/// For iOS and macOS this returns a `CupertinoWebSocket` wrapped in a
/// `AdapterWebSocketChannel` and `null` for the rest of the platforms.
///
/// It may return `null` because the differentiation for the other platforms
/// is done in [RealtimeClient].
WebSocketChannel? getPlatformWebSocketChannel(String url) {
if (Platform.isIOS || Platform.isMacOS) {
return AdapterWebSocketChannel(
CupertinoWebSocket.connect(Uri.parse(url)),
);
}
return null;
}
10 changes: 10 additions & 0 deletions packages/supabase_flutter/lib/src/platform_http_web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:http/http.dart' as http;
import 'package:web_socket_channel/web_socket_channel.dart';

http.Client getPlatformHttpClient() {
return http.Client();
}

WebSocketChannel? getPlatformWebSocketChannel(String url) {
return null;
}
17 changes: 16 additions & 1 deletion packages/supabase_flutter/lib/src/supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:supabase_flutter/src/flutter_go_true_client_options.dart';
import 'package:supabase_flutter/src/local_storage.dart';
import 'package:supabase_flutter/src/supabase_auth.dart';

import 'platform_http_io.dart'
if (dart.library.js_interop) 'platform_http_web.dart';
import 'version.dart';

final _log = Logger('supabase.supabase_flutter');
Expand Down Expand Up @@ -114,6 +116,13 @@ class Supabase with WidgetsBindingObserver {
),
);
}
if (realtimeClientOptions.webSocketTransport == null) {
final platformWebSocketChannel = getPlatformWebSocketChannel(url);
if (platformWebSocketChannel != null) {
realtimeClientOptions = realtimeClientOptions.copyWith(
webSocketTransport: (url, headers) => platformWebSocketChannel);
}
}
_instance._init(
url,
anonKey,
Expand Down Expand Up @@ -192,10 +201,16 @@ class Supabase with WidgetsBindingObserver {
...Constants.defaultHeaders,
if (customHeaders != null) ...customHeaders
};
final Client platformHttpClient;
if (httpClient != null) {
platformHttpClient = httpClient;
} else {
platformHttpClient = getPlatformHttpClient();
}
client = SupabaseClient(
supabaseUrl,
supabaseAnonKey,
httpClient: httpClient,
httpClient: platformHttpClient,
headers: headers,
realtimeClientOptions: realtimeClientOptions,
postgrestOptions: postgrestOptions,
Expand Down
2 changes: 2 additions & 0 deletions packages/supabase_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies:
shared_preferences: ^2.0.0
logging: ^1.2.0
web: '>=0.5.0 <2.0.0'
cupertino_http: '>=1.4.0 <3.0.0'
web_socket_channel: '>=2.3.0 <4.0.0'

dev_dependencies:
dart_jsonwebtoken: ^2.4.1
Expand Down