Python Tutorial: Introduction to Data streams
Key Takeaways
This video tutorial introduces the concept of data streams using MQTT, a publish/subscribe messaging protocol, and demonstrates how to subscribe to data streams using the Eclipse Paho MQTT Python client.
Full Transcript
welcome back in this lesson we'll learn how to subscribe to data streams using MQTT data streams are a constant stream of data examples of data streams include but are not limited to Twitter messages online news articles video streams such as this video sensor data or IOT and financial market orders this course will focus on sensor data from healthy devices we mentioned MQTT a couple of times already but what is it exactly n QT t or Message Queuing telemetry transport is a publish/subscribe messaging protocol designed to have a small footprint making it ideal for iot use cases an MQTT system consists of clients connecting to a server also called a broker these clients can publish or produce data to a topic and subscribe or consume data from or know multiple topics a topic is a string that the broker uses to filter messages for each connected client each topic can have multiple clients producing or consuming the same topic during this course will only act as a client subscribing to predefined topics assuming a producer is available in producing data will be using Eclipse paho and qtt Python client to connect to and PTT brokers the pipe modules are available in the package bajo dot MQTT more information and the documentation for the library is available under the project github page in the simplest case we only want one message from the broker this could be the case for other page showing the latest value available we can use subscribe that's simple for this which takes the topic as the first argument and the block has host name as the second argument and will return a message object we can then print the message topic as well as the message payload more often we want to receive a constant stream of data we are going to use a concept named callback to subscribe to a topic this is a very efficient way to wait for things to happen on the other end we do this by defining a callback function which will be called whenever a new message is available we are free to select any function name a recommended name is on message and will stick to that the header for a function used as the callback is defined by the PAHO dot MQTT library and needs to have the arguments client the client instance for the speaker back user data private user data set when creating the instance message an instance of MQTT message which is a class with the property's topic payload QoS and retain our arguments are mandatory even if we don't use them since the library calls the family to meet exactly these arguments within the function will print the topic of the message and the message payload separated by a column we now need to connect our function to an MQTT topic we'll import the module by hot mqtt dot subscribe and you subscribe to the callback with the function to be called as the first argument the topic to subscribe data camp / long term as the second argument and the server or broker as the keyword argument host name to connect a function to the topic the library will then connect to the MQTT broker and call our function each time a new message is available let's have a final look at a complete code first we import by hot mqtt dot subscribe a subscribe next we define our function on message and then pass this function as the first argument to subscribe that callback the topic name betacam slash room temp as the second argument and the host name for MK tts / as keyword argument host name once we run our program we will see the following output the topic on the left of the column as well as the message payload on the right which in this case is a JSON object containing the time temperature and humidity form an environmental sensor and now let's try this out
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/analyzing-iot-data-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Welcome back. In this lesson, we'll learn how to subscribe to datastreams using MQTT. Data streams are constant streams of data. Examples of data streams include, but are not limited to Twitter messages, online News Articles, Video streams such as this video, sensor data or IoT, and financial Market orders.
This course will focus on sensor data from IoT devices.
We mentioned MQTT a couple times already. But what is it exactly? MQTT, or Message Queuing Telemetry Transport, is a publish/subscribe messaging protocol, designed to have a small footprint, making it ideal for IoT use cases.
An MQTT system consists of clients connecting to a server, also called a Broker. These clients can publish (or produce) data to a topic, and subscribe (or consume) data from one or multiple topics.
A topic is a string that the Broker uses to filter messages for each connected client. Each topic can have multiple clients producing or consuming the same topic.
During this course, we'll only act as a client subscribing to predefined topics, assuming a producer is available and producing data.
We'll be using the Eclipse Paho MQTT Python client to connect to MQTT Brokers.
The Python modules are available in the package paho.mqtt.
More information and the documentation for the library is available under the projects Github page.
In the simplest case, we only want one message from the Broker.
This could be the case for a webpage showing the latest value available.
We can use subscribe.simple() for this, which takes the topic as the first argument, and the broker's hostname as the second argument and will return a message object.
We can then print the message topic, as well as the message payload.
More often, we'll want to receive a constant stream of data.
We're
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI