Visualizing MQTT interactions with shiftr.io

Visualizing MQTT interactions with shiftr.io

ยท

4 min read

Introduction

While I was wandering around the web looking for information related to MQTT, I came across a great service called shiftr.io that visualizes MQTT communication pretty well. This is a note when I gave it a try. It visualizes pretty much everything on MQTT like broker, clients, topics, the topology, message exchanges, message contents, communication volume, etc.

shiftr.io

It looks pretty interesting, right?

1.png

This is how it will be visualized.

  • Here is a sandbox-like environment (topic) where anyone can try it.
  • You can get there from Try on the top page of shiftr.io.
  • Legend
    • The large circle in the middle is the broker
    • The small white circle around it is a topic; MQTT can represent topics in a hierarchy like building/floor/room, so topics are attached to topics like branches.
    • The dotted circle is the client.
    • The moving black circle is the message

2.gif

Add a client

  • Try to run mqtt.rb, a client using ruby-mqtt.
  • When you run this, mySensor will come up and start communication. This client does subscribe to the same topic at [1] and publish at [2], so it looks like black circles go and come back
require 'rubygems'
require 'mqtt'

broker = "mqtt://try:try@broker.shiftr.io"
client_name = "mySensor"

MQTT::Client.connect(broker, client_id: client_name) do |client|
  client.subscribe '/example' # [1] topic_name to subscribe
  loop do
    client.publish 'example', 'hello' # [2] publish to topic_name, message
    while !client.queue_empty? do
      topic, message = client.get
      puts "#{topic}: #{message}"
    end
    sleep 1
  end end
end

Create a new Namespace

  • You can create your own namespace (topic) by selecting New Namespace from the Dashboard.
  • Once created, create a token from the Namespace Settings.
    • The token will be in a format like 74ge91e8:63ce1cf22a17765x.
    • If you put this before @ as mqtt://74ge91e8:63ce1cf22a17765x@broker.shiftr.io, you can send and receive messages to and from your Namespace.

More clients to play with

Topic name Tokyo with 3 pub and 10 sub.

3.gif

Here is the code for the client.

require 'rubygems'
require 'mqtt'

broker = "mqtt://<token>@broker.shiftr.io"
topic = "tokyo"
message = "hello world"

# Publisher
(101..103).each do |i|
  Process.fork do
    client_name = "sensor" + i.to_s
    MQTT::Client.connect(broker, client_id: client_name) do |client|
      loop do
        client.publish topic, message 
        sleep 2
      end end
    end end
  end end
end

# Subscriber
(1..10).each do |i|
  Process.fork do
    client_name = "sensor" + i.to_s
    MQTT::Client.connect(broker, client_id: client_name) do |client|
      client.subscribe topic 
      loop do
        while !client.queue_empty? do
          topic, message = client.get
          puts "#{client_name} #{topic} #{message}"
        end
        sleep 2
      end end
    end end
  end
end

Others

If you click on the circle of the client, you can see the communication volume and message contents.

  • Communication volume

4.png

  • Message

5.png

Summary

I thought it would be a good idea to design a topology with shiftr.io and write the code to simulate your sensor topology before going for the production, because it would be hard to understand if there are too many sensors in the real field. shiftr.io does pretty good job!