MQTT and Serverless Getting Started Guide for 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.

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.

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

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff