MQTT Guide for Serverless: Getting Started with IoT

4 min read Chandler Mayo on May 22, 2019
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

The MQTT standard was originally invented by IBM to efficiently transfer data between embedded systems. In 2010, IBM released a royalty-free version of the MQTT standard.

The MQTT standard was originally invented by IBM to efficiently transfer data between embedded systems. In 2010, IBM released a royalty-free version of the MQTT standard. The MQTT 3.1.1 standard became an OASIS standard in 2014. Today, developers routinely use MQTT for connecting IoT devices.

MQTT Guide: Why Use MQTT? Why Not HTTP?

HTTP and MQTT were designed to accomplish different tasks: MQTT is optimized for data. HTTP is for documents.

HTTP uses a request-response method to transmit documents. Both the server and the client must be available. HTTP is extendable and you can adapt it to your needs, but the flexibility often becomes a burden on mobile or IoT devices where power usage, efficiency, and limited connectivity are concerns.

MQTT uses a publish-subscribe method to send data. Clients can publish messages to topics and subscribe to receive updates as they happen. MQTT requires an intermediary, called a broker, to handle queuing and transmitting messages. Clients can come and go without disrupting the ability for messages to be transmitted and MQTT is designed to be lightweight.

MQTT is perfect for limited connectivity situations commonly encountered with IoT.

MQTT Guide: How does PubNub work with MQTT?iot-main-logo

PubNub provides an MQTT gateway that allows you to use all the features of our Data Stream Network, like Functions and wildcards, with MQTT. PubNub replaces using a broker for transmitting data with MQTT. PubNub maintains synchronized global Points of Presence (data centers) that can transfer data anywhere in the world with an internet connection in under 0.25 seconds. PubNub is great for latency sensitive applications.

Logitech uses PubNub for IoT applications.

Serverless IoT with PubNub Functions

PubNub Functions

PubNub offers a serverless environment to execute functions on messages as they route through the PubNub Network. You can incorporate logic like re-routing, augmenting, filtering, transforming, and aggregating data.

The Blocks Catalog is a growing collection of prebuilt microservices for enhancing your Internet of Things deployments.

PubNub has partnered with dozens of leading companies to provide easily integrable partner Blocks. You can initiate image recognition, visualize data, analyze sentiment, and more.

Getting Started with MQTT and PubNub

  • First, you’ll need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys from the PubNub Admin Dashboard. Sign up using the embedded form below:

Next, download MQTT.fx. We’ll use MQTT.fx to examine how the PubNub MQTT gateway works. This client simulates a device. Any other MQTT client would also work.

Add a new connection profile with the following:

1. Use a broker address of Use the standard ports for MQTT – for unsecured connections use 1883, for TLS secured connections use 8883 – both are supported.

2. Use a client ID composed like this:  <publish_key>/<subscribe_key>/<actual device ID>

Screenshot of MQTT.fx settingsConnect to PubNub and then click on the ‘Subscribe’ tab. Enter “test” for the topic (called a channel when using PubNub). Click ‘Subscribe’.

Go back to the ‘Publish’ tab and enter “test” for the topic. Enter some text in the text area. Click ‘Publish’. If you go back to the ‘Subscribe’ tab you’ll see your message.

MQTT Publish message MQTT Subscribe message

Channel / Topic Wildcards

PubNub’s MQTT support includes topic wildcard support.

If you have not already, enable the Stream Controller add-on, then click the ‘Enable Wildcard Subscribe checkbox.

When you use Wildcards with MQTT and PubNub, you must use /, but in PubNub you use .. If you want to provide access across the a channel / topic, you would type a/# in the MQTT client, but a.* in the PubNub Admin Dashboard or any applications using the Data Stream Network SDKs. The single-level and multi-level wildcards are the same, respectively + and #.

Wildcard subscriptions examples:

// Single-level wildcard will be translated to: .*
// Multi-level wildcard will be translated to: .house.sensor.*

Like this post? Check out How Industrial IoT and PubNub Can Be the Future of Predictive Maintenance.

Have suggestions or questions about the content of this post? Reach out at

More from PubNub

NPP and HIPAA: Notice of Privacy Practices Definition
Healthcare CategoryJan 6, 20235 min read

NPP and HIPAA: Notice of Privacy Practices Definition

A Notice of Privacy Practices (NPP) is one of the requirements of HIPAA and helps patients understand their personal data rights.

Michael Carroll

Michael Carroll

HIPAA Violation Examples
Healthcare CategoryJan 5, 20236 min read

HIPAA Violation Examples

HIPAA violations can be financially expensive and devastating to a brand. Examine some examples of HIPAA violations, and learn...

Michael Carroll

Michael Carroll

HIPAA Technical Safeguards: How To Protect Sensitive Data
Healthcare CategoryJan 5, 20236 min read

HIPAA Technical Safeguards: How To Protect Sensitive Data

HIPAA covered entities must follow the five technical safeguards to achieve HIPAA compliance and prevent data corruption.

Michael Carroll

Michael Carroll