8

I have a Mosquitto broker up and running on my Windows machine. I don't remember if I installed it with Web Sockets support (Cause I didn't knew what that was, or if I needed it). But seeing now my requirement is to use JavaScript (Paho) to connect to MQTT, I want to know how to enable Web Socket support for my existing MQTT broker.

I tried editing mosquitto.conf file by adding these lines to the file

listener 9001
protocol websockets

but doesn't seems to work. I am attaching an image that might provide a better picture:

Windows Command Prompt Error Messages and mosquitto.conf

I am not very sure of commands either but they seem to work with default port 1883.

So the question is: How do I make it work?

9

A few things:

  1. The mosquittto_pub and mosquitto_sub commands do not support WebSockets at all.

  2. When you run mosquitto from the command line you have to explicitly point to the config file with the -c option

    mosquitto -v -c /path/to/mosquitto.conf
    
  3. The Windows builds available from the mosquitto.org download pages don't support WebSockets, so the only way you will have WebSocket support is if you built mosquitto from source yourself and enable it.(as of version 1.5.1 the Windows builds now contain websocket support)

  • Would that mean that javascript won't be able to communicate with any pubsub client ? And, Any message sent to broker (publish) by javascript on a certain topic won't be recieved by mosquitto_sub listening on that topic? – Salim Shamim Mar 12 '17 at 14:39
  • With no WebSockets support the Paho javascript client will not be able to connect to the broker at all so it will be unable to publish or subscribe to any topics – hardillb Mar 12 '17 at 14:50
  • Ok, to quote this link (eclipse.org/paho/clients/js) "The Paho JavaScript Client is an MQTT browser-based client library written in Javascript that uses WebSockets to connect to an MQTT Broker." and this blog.ithasu.org/2016/05/… which explains how to enable WebSockets for Mosquitto on linux. I am not able to understand why It's not possible. I am sorry may be I am just lost. – Salim Shamim Mar 12 '17 at 15:28
  • 1
    That's already answered, the build for Windows does not have WebSockets compiled in. You will have to build it yourself if you want WebSocket support – hardillb Mar 12 '17 at 15:30
  • That's hard to hear. I've been trying to build a version of Mosquitto usnmg various references, but I'm always getting stuck somewhere. I've not been able to find anyone with a websocket-enabled mosquitto (windows-hosted) binary either. – Eric Wu Apr 28 '17 at 2:11
5

As @hardlib said, you have to compile mosquitto from sources. It took some trial and error for us to get it working on windows, so here some summary in case it helps somebody. Please don't hesitate to correct it:

  • Prerequisites

    • install Visual Studio
    • install cmake ( latest version is fine )
    • install OpenSSL to C:\temp\OpenSSL
    • unzip pthreads to C:\pthreads
  • Build libwebsockets:

    • git clone https://github.com/warmcat/libwebsockets.git and get into the project's dir
    • make sure you are in a stable commit, (2.3 is 6cf0a981667f0d225a9cf2d7aecfd6f50be6c1fb)
    • mkdir build && cd build
    • cmake -G "Visual Studio 15" ..
    • open the .sln in Visual Studio and set the build type to release and hit Ctrl + Shift + B
  • Build mosquitto

    • git clone https://github.com/eclipse/mosquitto.git
    • cd mosquitto
    • mkdir build && cd build
    • cmake -G "Visual Studio 15" ..
    • cmake-gui
    • make sure the websockets option is checked, and click on generate
    • open project (here comes the hacky part)
    • add the libwebsockets\lib and libwebsockets\build directories as additional include directories to the Release build of the mosquitto project and add libwebsockets\build\lib\Release to the additional library directories of the linker.
    • comment out #define snprintf sprintf_s from config.h
    • comment out the timespec struct from pthreads.h
    • remove optimized.lib and debug.lib from the additional libraries of the mosquitto_passwd project
    • add #include <Windows.h> to the win32 ifdef of mosquitto_passwd.c
    • Select release mode and hit Ctrl + Shift + B
2

I found a compiled version of Mosquitto with WebSockets built for Windows on GitHub which may be of use if you don't want to build the code yourself.

While it is documented in Korean, it also includes a PowerPoint presentation with instructions on how to build it yourself, if you do not wish to use the pre-compiled binaries.

2

Starting from 1.5.1 the windows package support websocket, see changelog https://mosquitto.org/blog/ You have just to edit mosquitto.conf file, specify to use the websocket protocol by adding "protocol websockets" (see definition around line 145) and eventually restart mosquitto if you run it as a service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.