Skip to content

Support running DWDS without a Chrome Debug Port (web-socket-based) #2639

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 24.3.12-wip

- Implemented a WebSocket-based communication protocol that provides essential developer tooling (hot reload, service extensions) when Chrome debugger access is unavailable. - [#2605](https://github.com/dart-lang/webdev/issues/2605)
- Added WebSocket-based hot reload and service extension support via new `WebSocketProxyService` class that implements VM service protocol over WebSockets.
- Created new files: `WebSocketProxyService`, `WebSocketDebugService`, `WebSocketAppDebugServices`, and `WebSocketDwdsVmClient` to support socket-based DWDS functionality.
- Enhanced `DevHandler` with `useWebSocketConnection` flag to toggle between Chrome-based and WebSocket-based communication protocols.

## 24.3.11

- Changed DWDS to always inject the client and added `useDwdsWebSocketConnection` flag to control communication protocol: when true uses socket-based implementation, when false uses Chrome-based communication protocol.
Expand Down
23 changes: 15 additions & 8 deletions dwds/lib/dart_web_debug_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class Dwds {
final DevHandler _devHandler;
final AssetReader _assetReader;
final bool _enableDebugging;
final bool _useDwdsWebSocketConnection;

Dwds._(
this.middleware,
this._devTools,
this._devHandler,
this._assetReader,
this._enableDebugging,
this._useDwdsWebSocketConnection,
) : handler = _devHandler.handler;

Stream<AppConnection> get connectedApps => _devHandler.connectedApps;
Expand All @@ -53,12 +55,18 @@ class Dwds {
await _assetReader.close();
}

/// Creates a debug connection for the given app connection.
///
/// Returns a [DebugConnection] that wraps the appropriate debug services
/// based on the connection type (WebSocket or Chrome-based).
Future<DebugConnection> debugConnection(AppConnection appConnection) async {
if (!_enableDebugging) throw StateError('Debugging is not enabled.');
final appDebugServices = await _devHandler.loadAppServices(appConnection);
final chromeProxyService = appDebugServices.chromeProxyService;
await chromeProxyService.isInitialized;
return DebugConnection(appDebugServices);

if (_useDwdsWebSocketConnection) {
return await _devHandler.createDebugConnectionForWebSocket(appConnection);
} else {
return await _devHandler.createDebugConnectionForChrome(appConnection);
}
}

static Future<Dwds> start({
Expand Down Expand Up @@ -123,10 +131,7 @@ class Dwds {
_logger.info('Serving DevTools at $uri\n');
}

final injected = DwdsInjector(
extensionUri: extensionUri,
useDwdsWebSocketConnection: useDwdsWebSocketConnection,
);
final injected = DwdsInjector(extensionUri: extensionUri);

final devHandler = DevHandler(
chromeConnection,
Expand All @@ -143,6 +148,7 @@ class Dwds {
debugSettings.spawnDds,
debugSettings.ddsPort,
debugSettings.launchDevToolsInNewWindow,
useWebSocketConnection: useDwdsWebSocketConnection,
);

return Dwds._(
Expand All @@ -151,6 +157,7 @@ class Dwds {
devHandler,
assetReader,
debugSettings.enableDebugging,
useDwdsWebSocketConnection,
);
}
}
4 changes: 4 additions & 0 deletions dwds/lib/data/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'error_response.dart';
import 'extension_request.dart';
import 'hot_reload_request.dart';
import 'hot_reload_response.dart';
import 'service_extension_request.dart';
import 'service_extension_response.dart';
import 'isolate_events.dart';
import 'register_event.dart';
import 'run_request.dart';
Expand All @@ -40,5 +42,7 @@ part 'serializers.g.dart';
ErrorResponse,
RegisterEvent,
RunRequest,
ServiceExtensionRequest,
ServiceExtensionResponse,
])
final Serializers serializers = _$serializers;
2 changes: 2 additions & 0 deletions dwds/lib/data/serializers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions dwds/lib/data/service_extension_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2025, the Dart project authors. All rights reserved.
// Defines the request for service extension calls over WebSocket.

import 'dart:convert';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'service_extension_request.g.dart';

abstract class ServiceExtensionRequest
implements Built<ServiceExtensionRequest, ServiceExtensionRequestBuilder> {
String get id;
String get method;
String
get argsJson; // Store args as JSON string for built_value compatibility

// Helper method to get args as Map<String, dynamic>
Map<String, dynamic> get args =>
argsJson.isEmpty
? <String, dynamic>{}
: json.decode(argsJson) as Map<String, dynamic>;

ServiceExtensionRequest._();
factory ServiceExtensionRequest([
void Function(ServiceExtensionRequestBuilder) updates,
]) = _$ServiceExtensionRequest;

// Convenient factory method to create with args Map
factory ServiceExtensionRequest.fromArgs({
required String id,
required String method,
required Map<String, dynamic> args,
}) => ServiceExtensionRequest(
(b) =>
b
..id = id
..method = method
..argsJson = json.encode(args),
);

static Serializer<ServiceExtensionRequest> get serializer =>
_$serviceExtensionRequestSerializer;
}
228 changes: 228 additions & 0 deletions dwds/lib/data/service_extension_request.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading