Extensions in Scratch let you add additional blocks to the palette. I’ve written about how to create extensions before, but in this post I want to share my latest extension which adds MQTT support.
I don’t have a particular Scratch project in mind for this yet, but publishing and subscribing to an MQTT broker from a Scratch project would allow multiple web browsers each running Scratch to communicate with each other. I’m sure there are some fun things this could be used for.
Connecting to an MQTT broker from Scratch
To connect to an MQTT broker:
- Add the new MQTT extension
- Choose which broker you want to connect to
- Click on Go to editor
MQTT blocks in Scratch
The extension adds three new blocks:
publish
– publishes a message to an MQTT topicnew message
– fires when a new message is received on an MQTT topicmessage
– contains the most recent message from an MQTT topic
Demonstrating the MQTT blocks in Scratch
Here is a simple project that shows the new blocks in action.
I have two sprites:
sender
This will publish an MQTT message when you click on the sprite
postman
This will display MQTT messages when they are received
Giving the project a try…
Communicating between Scratch browsers using MQTT
Sending and receiving a message in one browser isn’t very interesting, as Scratch already has blocks to send and receive events.
Where this gets interesting is in being able to communicate between multiple browsers, running on different machines.
A warning
All of the MQTT brokers I’m using in the extension are public. This means that every message that is published is available for anyone in the world to see. As such, this isn’t appropriate for publishing anything private or sensitive.
Ideas?
I’ve added this to the version of Scratch that I run at scratch.machinelearningforkids.co.uk.
What could this be used for? What sort of games and projects could be enabled by allowing multiple student projects to communicate with each other?
If you give anything like that a try, please let me know how you get on!