-
Notifications
You must be signed in to change notification settings - Fork 158
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
Add support for websocket #418
base: master
Are you sure you want to change the base?
Conversation
2121689
to
e177fa6
Compare
e177fa6
to
a74025a
Compare
Testing through this on fedora linux:
I may have missed something, but wolfssl with default configure gives wolfmqtt libwebsocket build error:
Building wolfssl with |
On my local system, I have libsockets built with wolfSSL support installed. I did run into issues with the workflow, and I just built wolfMQTT with The next effort for this will be to enable secure websocket support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far, just light feedback suggestions.
configure.ac
Outdated
@@ -380,6 +384,24 @@ if test "x$ENABLED_STRESS" != "xno"; then | |||
AM_CFLAGS="$AM_CFLAGS -DNUM_PUB_PER_TASK=$NUM_PUBS" | |||
fi | |||
|
|||
# WebSocket | |||
if test "x$enable_websocket" = "xyes" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed this configure warning:
./configure --enable-websocket
configure: WARNING: unrecognized options: --enable-websocket
checking build system type... x86_64-pc-linux-gnu
...
* Multi-thread: no
* Stress: no
* WebSocket: yes
Adding a missing AC_ARG_ENABLE([websocket]
fixes it:
diff --git a/configure.ac b/configure.ac
index 234a127..c24eaec 100644
--- a/configure.ac
+++ b/configure.ac
@@ -384,8 +384,14 @@ if test "x$ENABLED_STRESS" != "xno"; then
AM_CFLAGS="$AM_CFLAGS -DNUM_PUB_PER_TASK=$NUM_PUBS"
fi
+AC_ARG_ENABLE([websocket],
+ [AS_HELP_STRING([--enable-websocket],[Enable websocket example (default: disabled)])],
+ [ ENABLED_WEBSOCKET=$enableval ],
+ [ ENABLED_WEBSOCKET=no ]
+ )
+
# WebSocket
-if test "x$enable_websocket" = "xyes"
+if test "x$ENABLED_WEBSOCKET" = "xyes"
then
AM_CFLAGS="$AM_CFLAGS -DENABLE_MQTT_WEBSOCKET"
ENABLED_WEBSOCKET=yes
examples/net_libwebsockets.c
Outdated
|
||
/* Try to write with timeout */ | ||
ret = 0; | ||
while (ret <= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I try to connect while broker is down, it segfaults:
./examples/websocket/websocket_client localhost 19001
Connecting to localhost:19001
[2025/03/05 14:10:55:6780] N: lws_create_context: LWS: 4.3.3-no_hash, NET CLI SRV H1 H2 WS ConMon IPV6-on
[2025/03/05 14:10:55:6781] N: __lws_lc_tag: ++ [wsi|0|pipe] (1)
[2025/03/05 14:10:55:6781] N: __lws_lc_tag: ++ [vh|0|netlink] (1)
[2025/03/05 14:10:55:6782] N: __lws_lc_tag: ++ [vh|1|default||-1] (2)
[2025/03/05 14:10:55:6782] N: __lws_lc_tag: ++ [wsicli|0|WS/h1/default/localhost] (1)
[2025/03/05 14:10:55:6786] N: [wsicli|0|WS/h1/default/localhost]: lws_client_connect_check: getsockopt fd 6 says e 111
[2025/03/05 14:10:55:6787] N: [wsicli|0|WS/h1/default/localhost]: lws_client_connect_check: getsockopt fd 6 says e 111
[2025/03/05 14:10:55:6787] N: __lws_lc_untag: -- [wsicli|0|WS/h1/default/localhost] (0) 473μs
MqttClient_NetConnect failed: -8
[2025/03/05 14:10:55:6787] E: [wsicli|0|WS/h1/default/localhost]: lws_issue_raw: invalid sock
Segmentation fault (core dumped)
With this small change it errors gracefully:
- while (ret <= 0) {
+ while (ret <= 0 && net->status > 0) {
After:
./examples/websocket/websocket_client localhost 19001
Connecting to localhost:19001
[2025/03/05 14:10:30:3210] N: lws_create_context: LWS: 4.3.3-no_hash, NET CLI SRV H1 H2 WS ConMon IPV6-on
[2025/03/05 14:10:30:3211] N: __lws_lc_tag: ++ [wsi|0|pipe] (1)
[2025/03/05 14:10:30:3211] N: __lws_lc_tag: ++ [vh|0|netlink] (1)
[2025/03/05 14:10:30:3212] N: __lws_lc_tag: ++ [vh|1|default||-1] (2)
[2025/03/05 14:10:30:3212] N: __lws_lc_tag: ++ [wsicli|0|WS/h1/default/localhost] (1)
[2025/03/05 14:10:30:3215] N: [wsicli|0|WS/h1/default/localhost]: lws_client_connect_check: getsockopt fd 6 says e 111
[2025/03/05 14:10:30:3217] N: [wsicli|0|WS/h1/default/localhost]: lws_client_connect_check: getsockopt fd 6 says e 111
[2025/03/05 14:10:30:3217] N: __lws_lc_untag: -- [wsicli|0|WS/h1/default/localhost] (0) 482μs
MqttClient_NetConnect failed: -8
684697b
to
a428995
Compare
WebSocket Support
wolfMQTT supports MQTT over WebSockets, allowing clients to connect to MQTT brokers through WebSocket endpoints. This is useful for environments where traditional MQTT ports might be blocked or when integrating with web applications.
Building with WebSocket Support
To build wolfMQTT with WebSocket support:
Install the libwebsockets development library:
Configure wolfMQTT with WebSocket support:
Build as usual:
WebSocket Example
The WebSocket client example is located in
/examples/websocket/
. This example demonstrates how to connect to an MQTT broker using WebSockets. The example subscribes to the topic "test/topic" and waits for incoming messages.To run the example:
By default, it connects to
localhost
on port9001
.Broker Configuration
To use the WebSocket example, your MQTT broker must be configured to support WebSockets. For Mosquitto, add the following to your
mosquitto.conf
:Then restart Mosquitto with this configuration:
Implementation Details
The WebSocket implementation uses libwebsockets as the backend and provides custom network callbacks for the MQTT client:
NetWebsocket_Connect
: Establishes a WebSocket connection to the brokerNetWebsocket_Read
: Reads data from the WebSocket connectionNetWebsocket_Write
: Writes data to the WebSocket connectionNetWebsocket_Disconnect
: Closes the WebSocket connectionThe example also implements a ping mechanism to keep the WebSocket connection alive, sending a ping to the broker every 30 seconds.