Skip to content

Add support for custom WebSocket path #607

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

Merged
merged 2 commits into from
Jun 6, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ class MqttServerWs2Connection extends MqttServerWsConnection {
events.EventBus? eventBus,
List<RawSocketOption> socketOptions,
Duration? socketTimeout,
) : super(eventBus, socketOptions, socketTimeout);
String? websocketPath,
) : super(eventBus, socketOptions, socketTimeout, websocketPath);

/// Initializes a new instance of the MqttWs2Connection class.
MqttServerWs2Connection.fromConnect(
Expand All @@ -117,7 +118,7 @@ class MqttServerWs2Connection extends MqttServerWsConnection {
events.EventBus eventBus,
List<RawSocketOption> socketOptions,
Duration? socketTimeout,
) : super(eventBus, socketOptions, socketTimeout) {
) : super(eventBus, socketOptions, socketTimeout, null) {
connect(server, port);
}

Expand All @@ -143,6 +144,11 @@ class MqttServerWs2Connection extends MqttServerWsConnection {
}
uri = uri.replace(port: port);

// Add custom path if specified, otherwise default to /
if (super.websocketPath != null && super.websocketPath!.isNotEmpty) {
uri = uri.replace(path: super.websocketPath);
}

final uriString = uri.toString();
MqttLogger.log(
'MqttWs2Connection::connect - WS URL is $uriString, protocols are $protocols',
Expand Down Expand Up @@ -222,6 +228,11 @@ class MqttServerWs2Connection extends MqttServerWsConnection {
}
uri = uri.replace(port: port);

// Add custom path if specified, otherwise default to /
if (super.websocketPath != null && super.websocketPath!.isNotEmpty) {
uri = uri.replace(path: super.websocketPath);
}

final uriString = uri.toString();
MqttLogger.log(
'MqttWs2Connection::connectAuto - WS URL is $uriString, protocols are $protocols',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ class MqttServerWsConnection extends MqttServerConnection<WebSocket> {
/// User defined websocket headers
Map<String, dynamic>? headers;

/// User-defined websocket path.
String? websocketPath;

/// Default constructor
MqttServerWsConnection(
super.eventBus,
super.socketOptions,
super.socketTimeout,
this.websocketPath,
);

/// Initializes a new instance of the MqttConnection class.
Expand Down Expand Up @@ -61,6 +65,11 @@ class MqttServerWsConnection extends MqttServerConnection<WebSocket> {

uri = uri.replace(port: port);

// Add custom path if specified, otherwise default to /
if (websocketPath != null && websocketPath!.isNotEmpty) {
uri = uri.replace(path: websocketPath);
}

final uriString = uri.toString();
MqttLogger.log(
'MqttWsConnection::connect - WS URL is $uriString, protocols are $protocols',
Expand Down Expand Up @@ -124,6 +133,11 @@ class MqttServerWsConnection extends MqttServerConnection<WebSocket> {

uri = uri.replace(port: port);

// Add custom path if specified, otherwise default to /
if (websocketPath != null && websocketPath!.isNotEmpty) {
uri = uri.replace(path: websocketPath);
}

final uriString = uri.toString();
MqttLogger.log(
'MqttWsConnection::connectAuto - WS URL is $uriString, protocols are $protocols',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ class SynchronousMqttServerConnectionHandler
required super.socketOptions,
required super.socketTimeout,
reconnectTimePeriod = 5000,
this.websocketPath,
}) : super(maxConnectionAttempts: maxConnectionAttempts) {
connectTimer = MqttCancellableAsyncSleep(reconnectTimePeriod);
initialiseListeners();
}

/// User-defined websocket path.
String? websocketPath;

/// Synchronously connect to the specific Mqtt Connection.
@override
Future<MqttClientConnectionStatus> internalConnect(
Expand Down Expand Up @@ -56,6 +60,7 @@ class SynchronousMqttServerConnectionHandler
clientEventBus,
socketOptions,
socketTimeout,
websocketPath,
);
} else {
MqttLogger.log(
Expand All @@ -66,6 +71,7 @@ class SynchronousMqttServerConnectionHandler
clientEventBus,
socketOptions,
socketTimeout,
websocketPath,
);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/mqtt_server_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class MqttServerClient extends MqttClient {
}
}

/// User-defined websocket path.
String? websocketPath;

/// Initializes a new instance of the MqttServerClient class using the
/// default Mqtt Port.
/// The server hostname or URL to connect to
Expand Down Expand Up @@ -111,6 +114,7 @@ class MqttServerClient extends MqttClient {
if (useWebSocket) {
connectionHandler.secure = false;
connectionHandler.useWebSocket = true;
connectionHandler.websocketPath = websocketPath;
connectionHandler.useAlternateWebSocketImplementation =
useAlternateWebSocketImplementation;
if (connectionHandler.useAlternateWebSocketImplementation) {
Expand Down