Connecting to shiftr.io is simple

Try the examples below directly with the embedded testing namespace.

Use curl from the Terminal to post a message to the HTTP interface.

curl -X POST "http://try:try@broker.shiftr.io/example" -d "Hello world"

Use the MQTT.js library with node.js or in the browser to publish and subscribe using the MQTT interface:

// required when running on node.js
var mqtt = require('mqtt');

var client = mqtt.connect('mqtt://try:try@broker.shiftr.io', {
  clientId: 'javascript'
});

client.on('connect', function(){
  console.log('client has connected!');

  client.subscribe('/example');
  // client.unsubscribe('/example');

  setInterval(function(){
    client.publish('/hello', 'world');
  }, 1000);
});

client.on('message', function(topic, message) {
  console.log('new message:', topic, message.toString());
});

For more information see the Javascript manual in the documentation.

Use the Arduino and the arduino-mqtt library to publish and subscribe using the MQTT interface:

#include <Bridge.h>
#include <YunClient.h>
#include <MQTTClient.h>

YunClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  client.begin("broker.shiftr.io", net);

  connect();
}

void connect() {
  Serial.print("connecting...");
  while (!client.connect("arduino", "try", "try")) {
    Serial.print(".");
  }

  Serial.println("\nconnected!");

  client.subscribe("/example");
  // client.unsubscribe("/example");
}

void loop() {
  client.loop();

  if(!client.connected()) {
    connect();
  }

  // publish a message roughly every second.
  if(millis() - lastMillis > 1000) {
    lastMillis = millis();
    client.publish("/hello", "world");
  }
}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
}

For more information see the Arduino manual in the documentation.

Use Processing and the processing-mqtt library to publish and subscribe using the MQTT interface:

import mqtt.*;

MQTTClient client;

void setup() {
  client = new MQTTClient(this);
  client.connect("mqtt://try:try@broker.shiftr.io", "processing");
  client.subscribe("/example");
  // client.unsubscribe("/example");
}

void draw() {}

void keyPressed() {
  client.publish("/hello", "world");
}

void messageReceived(String topic, byte[] payload) {
  println("new message: " + topic + " - " + new String(payload));
}

For more information see the Processing manual in the documentation.

Use ruby and the ruby-mqtt library to publish and subscribe using the MQTT interface:

require 'rubygems'
require 'mqtt'

MQTT::Client.connect('mqtt://try:try@broker.shiftr.io', client_id: 'ruby') do |client|
  client.subscribe '/example'
  # client.unsubscribe '/example'

  loop do
    client.publish 'hello', 'world'

    while !client.queue_empty? do
      topic, message = client.get
      puts "#{topic}: #{message}"
    end

    sleep 1
  end
end

For more information see the Ruby manual in the documentation.