67

I am new to Mosquitto and MQTT, I downloaded the Mosquitto server library but I do not know how to test it.

Is there any way to test the Mosquitto server?

hardillb
  • 54,545
  • 11
  • 67
  • 105
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • A server can "smoke tested" (gross verification that it is running \ available): this is quick test to verify that the application is installed and running (not necessarily functional as it may need to be configured). In general, Smoke-tests are performed before a functional verification test are performed. An example is provided below – gatorback Aug 01 '19 at 16:17

6 Answers6

143

In separate terminal windows do the following:

  1. Start the broker:

    mosquitto
    
  2. Start the command line subscriber:

    mosquitto_sub -v -t 'test/topic'
    
  3. Publish test message with the command line publisher:

    mosquitto_pub -t 'test/topic' -m 'helloWorld'
    

As well as seeing both the subscriber and publisher connection messages in the broker terminal the following should be printed in the subscriber terminal:

test/topic helloWorld

EDIT:

It is worth pointing out that from v2.0.0 of Mosquitto it will only be listening for connections on the loopback interface by default. If you want to access the broker from machines other than the one it is installed on you will need to edit the config file (and pass it to the broker with the -c option e.g. mosquitto -c /path/to/mosquitto.conf) to enable listening on other interfaces. Details can be found in the v2.0.0 release notes here

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Actually, when i write mosquitto in the terminal, it says that no such command is recognised – Amrmsmb Nov 03 '14 at 14:50
  • 2
    Sounds like you haven't installed it. Update the original question with details of what steps you have actually done, then we may be able to help – hardillb Nov 03 '14 at 14:56
  • 1
    i just downlaoded the zip file and unzipped it, and there is no any excutable file to run – Amrmsmb Nov 03 '14 at 15:02
  • Which zip file for which platform? – hardillb Nov 03 '14 at 15:06
  • You haven't actually answered my question, but I'm going to assume you have downloaded the source code. In which case I suggest read the file called compiling.txt or go back to the mosquitto download page and download the windows binary listed lower down the page (http://mosquitto.org/files/binary/win32/mosquitto-1.3.5-install-win32.exe), in which case as it will be installed as a service you can miss out step 1 – hardillb Nov 03 '14 at 15:14
  • ok now i downloaded the binary file and installed it, but neither of commands posted are acceptable still unrecognisable – Amrmsmb Nov 03 '14 at 15:32
  • 1
    The mosquitto_pub and mosquitto_sub commands get installed to C:\Program Files (x86)\mosquitto (on Win 8) and you'll need to ensure that the service is running. Works fine here. – Andy Piper Nov 05 '14 at 14:22
  • 1
    if you installed mosquitto on Mac by Homebrew, the mosquitto locates in sbin folder – xmkevinchen Apr 11 '16 at 18:29
  • @xmkevincheb hmm, that seams wrong, there is no reason why mosquitto should have to run as root/admin – hardillb Apr 11 '16 at 19:18
26

Start the Mosquitto Broker
Open the terminal and type

mosquitto_sub -h 127.0.0.1 -t topic

Open another terminal and type
mosquitto_pub -h 127.0.0.1 -t topic -m "Hello"

Now you can switch to the previous terminal and there you can able to see the "Hello" Message.One terminal acts as publisher and another one subscriber.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Ashal
  • 391
  • 3
  • 3
15

To test and see if you can access your MQTT server from outside world (outside of your VM or local machine), you can install one of the MQTT publishing and monitoring tools such as MQTT-Spy on your outside-world machine and then subscribe for '#" (meaning all the topics).

You can follow this by the method @hardillb mentioned in his answer above and test back and forth such as this:

On the machine with Mosquitto Server running, enter image description here

On the outside-word machine with mqtt-spy running, enter image description here

I have mainly mentioned mqtt-spy since it's multi-platform and easy to use. You can go with any other tool really. And also to my knowledge to run the mosquitto_sub and mosquitto_pub you need to have mosquitto-clients installed on your Linux machine running the test (in my case Ubuntu) which can be done easily by,

sudo apt-get install mosquitto-clients

Mehrad
  • 4,093
  • 4
  • 43
  • 61
9

If you are using Windows, open up a command prompt and type 'netstat -an'.

If your server is running, you should be able to see the port 1883.

cmd displaying mosquitto port

If you cannot go to Task Manager > Services and start/restart the Mosquitto server from there. If you cannot find it here too, your installation of Mosquitto has not been successful.

A more detailed tutorial for setting up Mosquitto with Windows / is linked here.

rakidedigama
  • 714
  • 7
  • 11
1

The OP has not defined the scope of testing, however, simple (gross) 'smoke testing' an install should be performed before any time is invested with functionality testing.

How to test if application is installed ('smoke-test')

Log into the mosquitto server's command line and type:

mosquitto

If mosquitto is installed the machine will return:

 mosquitto version 1.4.8 (build date Wed, date of installation) starting
 Using default config.
 Opening ipv4 listen socket on port 1883
gatorback
  • 1,351
  • 4
  • 19
  • 44
Anshu
  • 1,277
  • 2
  • 13
  • 28
0

If you wish to have an GUI based broker testing without installing any tool you can use Hive Mqtt web socket for testing your Mosquitto server

just visit http://www.hivemq.com/demos/websocket-client/ and enter server connection details.

If you got connected means your server is configured properly.

You can also test publish and subscribe of messages using this mqtt web socket

Jayesh Singh
  • 696
  • 1
  • 9
  • 21
  • 1
    That only works if you have configured a Websocket Listener in your mosquitto.conf file. By default mosquitto does not start a Websocker listener. – hardillb Jul 01 '20 at 11:18
  • Yes, I forgot that point, this will not work with out of box installation, I will update question, Thanks – Jayesh Singh Jul 01 '20 at 11:41