Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable both TCP and web sockets in Mosquitto?

I have used the Paho Library to implement messaging through MQTT. In my mosquitto.conf file, I have the following:

listener 1883
protocol websockets

This configuration is working for web applications. When I tried to connect using Android and iOS with TCP, it gives the error, "Connection timed out". When I disable the protocol websockets line, it is working with Android and iOS, but then the webpage cannot connect.

When I try the test MQTT server, test.mosquitto.org, everything works fine.

Please tell me I need it to work with Android, iOS and web applications, supporting both WebSockets and TCP connections.

like image 562
rekha s Avatar asked Dec 22 '15 04:12

rekha s


2 Answers

You need to define two listeners, one for mqtt over tcp and one for websockets:

# this will listen for mqtt on tcp
listener 1883

# this will expect websockets connections
listener 8080
protocol websockets
like image 149
ralight Avatar answered Oct 26 '22 08:10

ralight


In short :

  • On the web (javascript) you need websocket support.
  • On Android / iOS , you can get way with the standard TCP listener.

Mosquitto, if you're running it locally or on your server, most likely doesn't have websocket support (even if you provide it in the configuration). Websocket support can be define at compile time. If it wasn't defined when your version was compiled you won't have support for it.

From the Mosquitto broker conf docs : "Websockets support is currently disabled by default at compile time. "

So you will need to build a version of mosquitto from source with websocket support. There are plenty of resources online that will help you out with that.

I've written up my experience with it in this blog post.

like image 44
ddewaele Avatar answered Oct 26 '22 07:10

ddewaele