Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

make compatible with flutter web #6

Merged
merged 6 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.0.7]

- fix: Web compatibility

## [0.0.6]

- fix: RetryTimer `_tries` is not initialized
Expand Down
15 changes: 9 additions & 6 deletions lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import 'dart:async';
import 'dart:convert';
import 'dart:core';

import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import 'constants.dart';
import 'message.dart';
import 'realtime_subscription.dart';
import 'retry_timer.dart';
import 'websocket_stub.dart'
if (dart.library.io) 'websocket_io.dart'
if (dart.library.html) 'websocket_web.dart';

typedef Logger = void Function(String kind, String msg, dynamic data);
typedef Encoder = void Function(
Expand Down Expand Up @@ -71,9 +73,7 @@ class RealtimeClient {
this.params = const {},
this.headers = const {},
}) : endPoint = '$endPoint/${Transports.websocket}',
transport = (transport ??
(url, headers) =>
IOWebSocketChannel.connect(url, headers: headers)) {
transport = transport ?? createWebSocketClient {
this.reconnectAfterMs = reconnectAfterMs ??
(int tries) {
return [1000, 2000, 5000, 10000][tries - 1] ?? 10000;
Expand Down Expand Up @@ -189,7 +189,8 @@ class RealtimeClient {
channels = channels.where((c) => c.joinRef() != channel.joinRef()).toList();
}

RealtimeSubscription channel(String topic, {Map<String, dynamic> chanParams = const {}}) {
RealtimeSubscription channel(String topic,
{Map<String, dynamic> chanParams = const {}}) {
final chan = RealtimeSubscription(topic, this, params: chanParams);
channels.add(chan);
return chan;
Expand All @@ -203,7 +204,8 @@ class RealtimeClient {
});
}

log('push', '${message.topic} ${message.event} (${message.ref})', message.payload);
log('push', '${message.topic} ${message.event} (${message.ref})',
message.payload);

if (isConnected()) {
callback();
Expand Down Expand Up @@ -288,6 +290,7 @@ class RealtimeClient {
/// communication has been closed
void _onConnClose(String event) {
log('transport', 'close', event);

/// SocketStates.disconnected: by user with socket.disconnect()
/// SocketStates.closed: NOT by user, should try to reconnect
if (connState == SocketStates.closed) {
Expand Down
7 changes: 7 additions & 0 deletions lib/src/websocket_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

WebSocketChannel createWebSocketClient(
String url, Map<String, String> headers) {
return IOWebSocketChannel.connect(url, headers: headers);
}
7 changes: 7 additions & 0 deletions lib/src/websocket_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:web_socket_channel/web_socket_channel.dart';

WebSocketChannel createWebSocketClient(
String url, Map<String, String> headers) {
throw UnimplementedError(
'Websocket Client not implemented for this platform');
}
7 changes: 7 additions & 0 deletions lib/src/websocket_web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:web_socket_channel/html.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

WebSocketChannel createWebSocketClient(
String url, Map<String, String> headers) {
return HtmlWebSocketChannel.connect(url);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: realtime_client
description: Listens to changes in a PostgreSQL Database and via websockets. This is for usage with Supabase Realtime server.
version: 0.0.6
version: 0.0.7
homepage: "https://supabase.io"
repository: "https://github.com/supabase/realtime-dart"

Expand Down
19 changes: 11 additions & 8 deletions test/socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:web_socket_channel/web_socket_channel.dart';

import 'socket_test_stubs.dart';

typedef IOWebSocketChannelClosure = IOWebSocketChannel Function(
typedef WebSocketChannelClosure = WebSocketChannel Function(
String url, Map<String, String> headers);

void main() {
Expand Down Expand Up @@ -53,7 +53,7 @@ void main() {
'error': [],
'message': [],
});
expect(socket.transport is IOWebSocketChannelClosure, true);
expect(socket.transport is WebSocketChannelClosure, true);
expect(socket.timeout, const Duration(milliseconds: 10000));
expect(socket.longpollerTimeout, 20000);
expect(socket.heartbeatIntervalMs, 30000);
Expand All @@ -78,7 +78,7 @@ void main() {
'error': [],
'message': [],
});
expect(socket.transport is IOWebSocketChannelClosure, true);
expect(socket.transport is WebSocketChannelClosure, true);
expect(socket.timeout, const Duration(milliseconds: 40000));
expect(socket.longpollerTimeout, 50000);
expect(socket.heartbeatIntervalMs, 60000);
Expand All @@ -97,14 +97,15 @@ void main() {
});

test('returns endpoint with parameters', () {
final socket = RealtimeClient('ws://example.org/chat', params: {'foo': 'bar'});
final socket =
RealtimeClient('ws://example.org/chat', params: {'foo': 'bar'});
expect(socket.endPointURL(),
'ws://example.org/chat/websocket?foo=bar&vsn=1.0.0');
});

test('returns endpoint with apikey', () {
final socket =
RealtimeClient('ws://example.org/chat', params: {'apikey': '123456789'});
final socket = RealtimeClient('ws://example.org/chat',
params: {'apikey': '123456789'});
expect(socket.endPointURL(),
'ws://example.org/chat/websocket?apikey=123456789&vsn=1.0.0');
});
Expand Down Expand Up @@ -318,7 +319,8 @@ void main() {
mockedSocket.connect();
mockedSocket.connState = SocketStates.open;

final message = Message(topic: topic, payload: payload, event: event, ref: ref);
final message =
Message(topic: topic, payload: payload, event: event, ref: ref);
mockedSocket.push(message);

verify(mockedSink.add(jsonData));
Expand All @@ -330,7 +332,8 @@ void main() {

expect(mockedSocket.sendBuffer.length, 0);

final message = Message(topic: topic, payload: payload, event: event, ref: ref);
final message =
Message(topic: topic, payload: payload, event: event, ref: ref);
mockedSocket.push(message);

verifyNever(mockedSink.add(jsonData));
Expand Down