Skip to content

Commit 7576837

Browse files
kanatxsahil03x
andauthored
feat(llc): allow overridding websocket baseUrl (#2093)
Co-authored-by: Sahil Kumar <sahil@getstream.io>
1 parent 7ed1709 commit 7576837

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

packages/stream_chat/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
## Upcoming
2+
3+
✅ Added
4+
5+
- Added support for overriding the `baseUrl` of the websocket.
6+
7+
```dart
8+
final client = StreamChatClient(
9+
apiKey,
10+
logLevel: Level.INFO,
11+
baseWsUrl: 'http://localhost:8080',
12+
);
13+
```
14+
115
## 9.2.0
216

317
- Bug fixes and improvements

packages/stream_chat/lib/src/client/client.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class StreamChatClient {
6969
this.logHandlerFunction = StreamChatClient.defaultLogHandler,
7070
RetryPolicy? retryPolicy,
7171
String? baseURL,
72+
String? baseWsUrl,
7273
Duration connectTimeout = const Duration(seconds: 6),
7374
Duration receiveTimeout = const Duration(seconds: 6),
7475
StreamChatApi? chatApi,
@@ -102,7 +103,7 @@ class StreamChatClient {
102103
_ws = ws ??
103104
WebSocket(
104105
apiKey: apiKey,
105-
baseUrl: options.baseUrl,
106+
baseUrl: baseWsUrl ?? options.baseUrl,
106107
tokenManager: _tokenManager,
107108
handler: handleEvent,
108109
logger: detachedLogger('🔌'),

packages/stream_chat/lib/src/core/api/moderation_api.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dart:convert';
22

3-
import 'package:stream_chat/src/core/http/stream_http_client.dart';
43
import 'package:stream_chat/stream_chat.dart';
54

65
/// Defines the api dedicated to moderation operations

packages/stream_chat/lib/src/ws/websocket.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,26 @@ class WebSocket with TimerHelper {
165165
'stream-auth-type': token.authType.name,
166166
...queryParameters,
167167
};
168-
final scheme = baseUrl.startsWith('https') ? 'wss' : 'ws';
169-
final host = baseUrl.replaceAll(RegExp(r'(^\w+:|^)\/\/'), '');
168+
169+
final scheme = switch (baseUrl) {
170+
final url when url.startsWith(RegExp('^(ws?|http?)://')) => 'ws',
171+
final url when url.startsWith(RegExp('^(wss?|https?)://')) => 'wss',
172+
_ => throw const FormatException('Invalid url format'),
173+
};
174+
175+
final address = baseUrl.replaceAll(RegExp(r'(^\w+:|^)\/\/'), '');
176+
final (:host, :port) = switch (address.split(':')) {
177+
[final host] => (host: host, port: null),
178+
[final host, final port] => (host: host, port: int.tryParse(port)),
179+
_ => throw const FormatException('Invalid address format'),
180+
};
181+
182+
_logger?.info('[buildUri] #ws; scheme: $scheme, host: $host, port: $port');
183+
170184
return Uri(
171185
scheme: scheme,
172186
host: host,
187+
port: port,
173188
pathSegments: ['connect'],
174189
queryParameters: qs,
175190
);
@@ -199,6 +214,7 @@ class WebSocket with TimerHelper {
199214
final uri = await _buildUri(
200215
includeUserDetails: includeUserDetails,
201216
);
217+
_logger?.info('[connect] #ws; uri: $uri');
202218
_initWebSocketChannel(uri);
203219
} catch (e, stk) {
204220
_onConnectionError(e, stk);
@@ -230,6 +246,7 @@ class WebSocket with TimerHelper {
230246
refreshToken: refreshToken,
231247
includeUserDetails: false,
232248
);
249+
_logger?.info('[reconnect] #ws; uri: $uri');
233250
try {
234251
_initWebSocketChannel(uri);
235252
} catch (e, stk) {
@@ -350,7 +367,8 @@ class WebSocket with TimerHelper {
350367
}
351368

352369
void _onConnectionError(error, [stacktrace]) {
353-
_logger?.warning('Error occurred', error, stacktrace);
370+
_logger?.warning(
371+
'[onConnectionError] #ws; error occurred', error, stacktrace);
354372

355373
StreamWebSocketError wsError;
356374
if (error is WebSocketChannelException) {

packages/stream_chat/lib/stream_chat.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export 'src/core/api/responses.dart';
2525
export 'src/core/api/stream_chat_api.dart';
2626
export 'src/core/error/error.dart';
2727
export 'src/core/http/interceptor/logging_interceptor.dart';
28+
export 'src/core/http/stream_http_client.dart';
2829
export 'src/core/models/action.dart';
2930
export 'src/core/models/attachment.dart';
3031
export 'src/core/models/attachment_file.dart';

packages/stream_chat/test/src/core/http/interceptor/auth_interceptor_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'package:dio/dio.dart';
44
import 'package:mocktail/mocktail.dart';
55
import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart';
66
import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart';
7-
import 'package:stream_chat/src/core/http/stream_http_client.dart';
87
import 'package:stream_chat/src/core/http/token.dart';
98
import 'package:stream_chat/src/core/http/token_manager.dart';
109
import 'package:stream_chat/stream_chat.dart';

packages/stream_chat/test/src/ws/websocket_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main() {
2929

3030
webSocket = WebSocket(
3131
apiKey: 'api-key',
32-
baseUrl: 'base-url',
32+
baseUrl: 'ws://<local-ip>:8800',
3333
tokenManager: tokenManager,
3434
webSocketChannelProvider: channelProvider,
3535
);

0 commit comments

Comments
 (0)