Skip to content

Commit 12a6645

Browse files
committed
Support mqttv3.1 WebSocket subprotocol in addition to mqtt
Addresses #15001 The Web MQTT handler only accepts the `mqtt` WebSocket subprotocol, rejecting clients that request `mqttv3.1`. This prevents some MQTT clients from establishing WebSocket connections. This change updates the protocol negotiation to accept either `mqtt` or `mqttv3.1` subprotocols. The handler uses `lists:search/2` to find the first matching protocol and echoes that exact value back in the `sec-websocket-protocol` response header, ensuring proper protocol negotiation with the client.
1 parent 0e38285 commit 12a6645

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ init(Req, Opts) ->
106106
undefined ->
107107
no_supported_sub_protocol(undefined, Req);
108108
Protocol ->
109-
case lists:member(<<"mqtt">>, Protocol) of
109+
case lists:search(fun(P) -> P =:= <<"mqtt">> orelse P =:= <<"mqttv3.1">> end, Protocol) of
110110
false ->
111111
no_supported_sub_protocol(Protocol, Req);
112-
true ->
113-
Req1 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, <<"mqtt">>, Req),
112+
{value, MatchedProtocol} ->
113+
Req1 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, MatchedProtocol, Req),
114114
State = #state{socket = maps:get(proxy_header, Req, undefined),
115115
stats_timer = rabbit_event:init_stats_timer()},
116116
WsOpts0 = proplists:get_value(ws_opts, Opts, #{}),

0 commit comments

Comments
 (0)