bind address for websockets #170

minghuadev opened this Issue May 25, 2016 · 7 comments


None yet

3 participants

minghuadev commented May 25, 2016 edited

Hi, Trying to run mosqitto on openshift which would allow only to use a virtual IP address thus need to bind to an IP address in addition to a port. It failed. Is there a way of doing it?

Googled an email by Chris on Sep2 2015 on the dev mainling list suggesting use of "iface" parameter. Has that been done?

Following the information from the mailing list post eventually got this diff to have made it work:

diff --git a/src/websockets.c b/src/websockets.c
index d79fabf..839f314 100644
--- a/src/websockets.c
+++ b/src/websockets.c
@@ -627,6 +627,18 @@ struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listen

        lws_set_log_level(log_level, log_wrap);

+    if ( listener->host ) {
+        size_t sz = strnlen(listener->host, 16);
+        if ( sz >=7 && sz <= 16 ) { /* IP in string: 7 min length. 15 max. */
+            info.iface = listener->host;
+               _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, 
+                        "Listener host \"%s\" of size %zu.", listener->host, sz);
+        } else {
+               _mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, 
+                        "Warning: Malformed listener host \"%s\" of size %zu.", 
+                                  listener->host, sz);
+        }
+    }
        _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener
        return libwebsocket_create_context(&info);

Does this or something similar need to be patched to mosquitto?


ralight commented May 25, 2016

I think a better approach would to just add info.iface = listener->host and have listeners for websockets just pass an interface name instead of an ip address/hostname.

minghuadev commented May 25, 2016 edited

Hi, Roger, Yes making it simply info.iface = listener->host looks better but I don't have the knowledge right now to assume that's safe enough.

As to passing in an interface name, it is not possible on an openshift gear. The openshift online version puts a gear into a guarded environment and reroute the traffic to an internal 127.x.x.x address. The address is passed to the user application by an environment variable, e.g. OPENSHIFT_DIY_IP, so that the user app can bind to. Passing in the IP address as an interface name will just work, which has been verified with the libwebsockets author via a ticket and also verified by running mosquitto/libwebsockets on a local machine and on the openshfit gear.

For short, adding info.iface = listener->host will be all needed from mosquitto.

By the way, very much thanks for the great work making mosquitto possible and open to public!


A note about the actual configuration used so not to leave any confusion. It is created by bash:

echo listener $OPENSHIFT_DIY_PORT $OPENSHIFT_DIY_IP > mosquitto.conf
echo protocol websockets  >> mosquitto.conf
echo http_dir simpleweb   >> mosquitto.conf
echo connection_messages true >> mosquitto.conf

In the sample configuration it comments:

# listener port-number [ip address/host name]

So the part [ip address/host name] can actually take [ip address/host name/iface name] for websockets protocol.

@minghuadev minghuadev added a commit to minghuadev/mosquitto that referenced this issue May 26, 2016
@minghuadev minghuadev allow websocket bind to ip address. issue #170
the change based on discussion on issue #170
@ralight ralight added a commit that referenced this issue May 26, 2016
@ralight ralight [170] Fix WS listeners not being able to bind to an IP.
Closes #170. Thanks to minghuadev.

Bug: #170
ralight commented May 26, 2016

There you go.

Note that I'm not referring to interface names in the documentation because although this is possible now, it's something that may change in the future.

@ralight ralight closed this May 26, 2016

Thank you Roger!

HenryNe commented Oct 7, 2016

Hello, the issue is still alive in version 1.4.10. The address is still ignored. Used Debian binary mosquitto_1.4.10-0mosquitto4_amd64.deb from, running on vServer with multiple network interfaces.

For simple test you can use Here are minimal steps to reproduce it:

echo "listener 1234" >/etc/mosquitto-websocket.conf
echo "protocol websockets" >>/etc/mosquitto-websocket.conf
mosquitto -v -c /etc/mosquitto-websocket.conf
1475840402: mosquitto version 1.4.10 (build date Thu, 25 Aug 2016 10:12:09 +0100) starting
1475840402: Config loaded from /etc/mosquitto-websocket.conf.
1475840402: Opening websockets listen socket on port 1234.

Open a second terminal to check binded interfaces:

netstat -anp | grep mosquitto
tcp   0   0*   LISTEN   21900/mosquitto

Compairing with protocol mqtt:

echo "listener 1234" >/etc/mosquitto-mqtt.conf
echo "protocol mqtt" >>/etc/mosquitto-mqtt.conf
mosquitto -v -c /etc/mosquitto-mqtt.conf
1475840579: mosquitto version 1.4.10 (build date Thu, 25 Aug 2016 10:12:09 +0100) starting
1475840579: Config loaded from /etc/mosquitto-mqtt.conf.
1475840579: Opening ipv4 listen socket on port 1234.

Check opened ports in second terminal:

netstat -anp | grep mosquitto
tcp   0   0*   LISTEN   21905/mosquitto

HenryNe, Please probably open a new issue to keep track of if it has not been done yet. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment