Over a million developers have joined DZone.

Top 5 Application Examples for Mongoose (Excerpt: MQTT)

Want to get started with Mongoose, but need a little help? In this post, we'll provide an example covering how to use Mongoose with MQTT.

· IoT Zone

Access the survey results 'State of Industrial Internet Application Development' to learn about latest challenges, trends and opportunities with Industrial IoT, brought to you in partnership with GE Digital.


Mongoose is known as one of the best embedded web servers currently available. From starting as a single-click-to-run executable to now being an embedded networking library, its functions range from WebSocket to MQTT and more.

To make sure you get off to an easy start with Mongoose, we have examples for almost all of its functionality. You can easily follow these, amend them, and use them for your integration project. 

In this ebook, you'll find the top 5 application examples with full code outlines and additional reference.

Learn how to set up:

  1. WebSocket Chat
  2. RESTful Server
  3. Big Upload
  4. MQTT Broker and Client
  5. Captive DNS Server

Find the code and reference compiled in one handy ebook: Top 5 Mongoose Embedded Web Server Examples.

Now for the sample excerpt...

MQTT Broker and Client

Here, we’d like to show you how you can use Mongoose to add one of the most popular M2M protocols to your product: MQTT.

We’re not going to spend much time singing the praises of MQTT, as you can find much more in-depth material about it online already. Yet, there are couple of things that are worth spelling out before you head out Googling for the merits of MQTT over whatnot:

  • Lightweight: simple binary protocol, small overhead, built to be friendly to small devices, where there is not much room for big buggers nor sophisticated code.

  • Simple: you can actually read the spec. It’s not like reading a phonebook, you know, a lot of characters but a lousy plot; no, the MQTTspec just tells it all, right there.

  • Standard: born at IBM, now MQTT is an OASIS standard. There are a bunch of different implementations of MQTT brokers and client libraries, with good interoperability.

  • Pub/Sub: the underlying communication model is that of message queues and the publish-subscribe paradigm. Queuing means you can reach your target even if it was offline. Pub-Sub means you don’t need to know whom to send what; whoever is interested in what you have to say will subscribe to the topic.

So let’s dig straight into our example: mqtt_client.c

In order to run the example, you need to run an MQTT broker, the server software that coordinates. There are many open source MQTT brokers out there. Mongoose is a generic library MQTT protocol library and can be used to implement brokers as well.

For convenience, we also provide a simple broker implementation in mqtt_broker.c; here’s how you can run it:

$ cd examples/mqtt_broker
$ make
$ ./mqtt_broker

Once you have a broker running, let’s try out our example client:

$ cd examples/mqtt_client
$ make
$ ./mqtt_client
Subscribing to '/stuff'
Subscription acknowledged, forwarding to '/test'

The goal of the example MQTT client app is to subscribe to messages coming from a topic called “/stuff” and forward each received message to the topic “/test”. In order to achieve this, it performs a TCP connection to the MQTT broker listening on port 1883 and sends a SUBSCRIBE command signaling its interest for the "/stuff" topic to the broker.

Enough jibber jabber, let’s see this in action!

While we are at it, let’s also use an MQTT client from another vendor, for example the very well known Mosquitto client, from the Eclipse project. Open a new shell and after having installed Mosquitto client, type:

$ mosquitto_pub -t /stuff -m foo

What happens here is that mosquitto_pub connects to the Mongoosemqtt_broker process and sends a PUBLISH command on the topic “/stuff”with the message “foo”. The broker knows that our mqtt_example has subscribed to that topic, and thus forwards that PUBLISH command through the connection established previously between the mqtt_clientprocess and the broker.

The Mongoose mqtt_client example will now receive this message and log the following message:

Got incoming message /stuff: foo
Forwarding to /test

Since nobody subscribed to the “/test” topic, the broker will just swallow this message, unless somebody subscribes to it as well.

If you want to see the forwarded message with your own eyes, why don’t you download Mongoose, clone mqtt_example.c and write a simple toy app that will subscribe to “/test” and print out all it gets there?

You can find more info in our MQTT reference documentation. We even have a JavaScript MQTT API for our Mongoose IoT Platform.

Want More?

Go ahead and download the full e-book: Top 5 Mongoose Embedded Web Server Examples.

The IoT Zone is brought to you in partnership with GE Digital.  Discover how IoT developers are using Predix to disrupt traditional industrial development models.

mongoose,mqtt,websocket,restful server,captive dns server

Published at DZone with permission of Sergey Lyubka, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}