Skip to content

Commit

Permalink
feat(llc): allow overridding websocket baseUrl (#2093)
Browse files Browse the repository at this point in the history
Co-authored-by: Sahil Kumar <[email protected]>
  • Loading branch information
kanat and xsahil03x authored Jan 25, 2025
1 parent 7ed1709 commit 7576837
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
14 changes: 14 additions & 0 deletions packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## Upcoming

✅ Added

- Added support for overriding the `baseUrl` of the websocket.

```dart
final client = StreamChatClient(
apiKey,
logLevel: Level.INFO,
baseWsUrl: 'http://localhost:8080',
);
```

## 9.2.0

- Bug fixes and improvements
Expand Down
3 changes: 2 additions & 1 deletion packages/stream_chat/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class StreamChatClient {
this.logHandlerFunction = StreamChatClient.defaultLogHandler,
RetryPolicy? retryPolicy,
String? baseURL,
String? baseWsUrl,
Duration connectTimeout = const Duration(seconds: 6),
Duration receiveTimeout = const Duration(seconds: 6),
StreamChatApi? chatApi,
Expand Down Expand Up @@ -102,7 +103,7 @@ class StreamChatClient {
_ws = ws ??
WebSocket(
apiKey: apiKey,
baseUrl: options.baseUrl,
baseUrl: baseWsUrl ?? options.baseUrl,
tokenManager: _tokenManager,
handler: handleEvent,
logger: detachedLogger('🔌'),
Expand Down
1 change: 0 additions & 1 deletion packages/stream_chat/lib/src/core/api/moderation_api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/stream_chat.dart';

/// Defines the api dedicated to moderation operations
Expand Down
24 changes: 21 additions & 3 deletions packages/stream_chat/lib/src/ws/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,26 @@ class WebSocket with TimerHelper {
'stream-auth-type': token.authType.name,
...queryParameters,
};
final scheme = baseUrl.startsWith('https') ? 'wss' : 'ws';
final host = baseUrl.replaceAll(RegExp(r'(^\w+:|^)\/\/'), '');

final scheme = switch (baseUrl) {
final url when url.startsWith(RegExp('^(ws?|http?)://')) => 'ws',
final url when url.startsWith(RegExp('^(wss?|https?)://')) => 'wss',
_ => throw const FormatException('Invalid url format'),
};

final address = baseUrl.replaceAll(RegExp(r'(^\w+:|^)\/\/'), '');
final (:host, :port) = switch (address.split(':')) {
[final host] => (host: host, port: null),
[final host, final port] => (host: host, port: int.tryParse(port)),
_ => throw const FormatException('Invalid address format'),
};

_logger?.info('[buildUri] #ws; scheme: $scheme, host: $host, port: $port');

return Uri(
scheme: scheme,
host: host,
port: port,
pathSegments: ['connect'],
queryParameters: qs,
);
Expand Down Expand Up @@ -199,6 +214,7 @@ class WebSocket with TimerHelper {
final uri = await _buildUri(
includeUserDetails: includeUserDetails,
);
_logger?.info('[connect] #ws; uri: $uri');
_initWebSocketChannel(uri);
} catch (e, stk) {
_onConnectionError(e, stk);
Expand Down Expand Up @@ -230,6 +246,7 @@ class WebSocket with TimerHelper {
refreshToken: refreshToken,
includeUserDetails: false,
);
_logger?.info('[reconnect] #ws; uri: $uri');
try {
_initWebSocketChannel(uri);
} catch (e, stk) {
Expand Down Expand Up @@ -350,7 +367,8 @@ class WebSocket with TimerHelper {
}

void _onConnectionError(error, [stacktrace]) {
_logger?.warning('Error occurred', error, stacktrace);
_logger?.warning(
'[onConnectionError] #ws; error occurred', error, stacktrace);

StreamWebSocketError wsError;
if (error is WebSocketChannelException) {
Expand Down
1 change: 1 addition & 0 deletions packages/stream_chat/lib/stream_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export 'src/core/api/responses.dart';
export 'src/core/api/stream_chat_api.dart';
export 'src/core/error/error.dart';
export 'src/core/http/interceptor/logging_interceptor.dart';
export 'src/core/http/stream_http_client.dart';
export 'src/core/models/action.dart';
export 'src/core/models/attachment.dart';
export 'src/core/models/attachment_file.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:dio/dio.dart';
import 'package:mocktail/mocktail.dart';
import 'package:stream_chat/src/core/http/interceptor/auth_interceptor.dart';
import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/http/token.dart';
import 'package:stream_chat/src/core/http/token_manager.dart';
import 'package:stream_chat/stream_chat.dart';
Expand Down
2 changes: 1 addition & 1 deletion packages/stream_chat/test/src/ws/websocket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {

webSocket = WebSocket(
apiKey: 'api-key',
baseUrl: 'base-url',
baseUrl: 'ws://<local-ip>:8800',
tokenManager: tokenManager,
webSocketChannelProvider: channelProvider,
);
Expand Down

0 comments on commit 7576837

Please sign in to comment.