Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

39
LINES

< > BotCompany Repo | #1026859 // MQTT Subscriber Spike

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1103K of libraries. Click here for Pure Java version (1908L/11K).

!7

lib 1400324 // Eclipse Paho (MQTT)

import org.eclipse.paho.client.mqttv3.*;

cmodule MQTTSubscriberSpike > DynPrintLog {
  switchable S topic = "iot_data";
  transient MqttClient client;
  
  start-thread {
    client = new MqttClient("tcp://localhost:1883", MqttClient.generateClientId());
    client.setCallback(new MyCallback);
    client.connect();
    client.subscribe(topic);
    print("Connected & subscribed to " + topic);
  }
  
  void cleanMeUp_mqtt ctex {
    if (client != null) {
      client.disconnect();
      client.close();
    }
  }
  
  class MyCallback implements MqttCallback {
    public void connectionLost(Throwable throwable) enter {
      print("Connection to MQTT broker lost!");
    }

    public void messageArrived(S s, MqttMessage mqttMessage) enter {
      print("Message received: "+ fromUtf8(mqttMessage.getPayload()));
    }

    public void deliveryComplete(IMqttDeliveryToken token) enter {
      print("Delivery complete: " + token);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026859
Snippet name: MQTT Subscriber Spike
Eternal ID of this version: #1026859/8
Text MD5: 5f010e47ba3611f85d8919d5813fc385
Transpilation MD5: 14eae3d161fb3b9c325bfdbc72a1eb2a
Author: stefan
Category: javax / mqtt
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-29 14:36:43
Source code size: 1029 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 265 / 1359
Version history: 7 change(s)
Referenced in: #1026860 - MQTT Subscriber & Publisher Spike (also used in Smart Home Demo)