0

We decided to use mqtt protocol for chat module in our mobile application. I want to save messages of topic in server side also. But i saw,mqtt client is global here,so one way is i have to subscribe single instance of mqtt client to all topics and save messages in database. but is it right approach to do it. i am just worring about it.

private void buildClient(){
        log.debug("Connecting... "+CLIENT_ID);
        try {
            mqttClient = new MqttClient(envConfiguration.getBrokerUrl(), CLIENT_ID);
        } catch (MqttException e) {
            log.debug("build client stopped due to "+e.getCause());
        }
        chatCallback = new ChatCallback();
        mqttClient.setCallback(chatCallback);
        mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setCleanSession(false);
    }

    @Override
    public void connect() {
        if(mqttClient == null || !mqttClient.getClientId().equals(CLIENT_ID)){
            buildClient();
        }
        boolean tryConnecting = true;
        while(tryConnecting){
            try {
                mqttClient.connect(mqttConnectOptions);
            } catch (Exception e) {
                log.debug("connection attempt failed "+ e.getCause() + " trying...");
            }
            if(mqttClient.isConnected()){
                tryConnecting = false;
            }else{
                pause();
            }
        }
    }


    @Override
    public void publish() {
        boolean publishCallCompletedErrorFree = false;
        while (!publishCallCompletedErrorFree) {
            try {
                mqttClient.publish(TOPIC, "hello".getBytes(), 1, true);
                publishCallCompletedErrorFree = true;
            } catch (Exception e) {
                log.debug("error occured while publishing "+e.getCause());
            }finally{
                pause();
            }
        }   
    }

    @Override
    public void subscribe() {
        if(mqttClient != null && mqttClient.isConnected()){
            try {
                mqttClient.subscribe(TOPIC, 2);
            } catch (MqttException e) {
                log.debug("subscribing error.."+e.getCause());
            }
        }

    }

    @Override
    public void disconnect() {
        System.out.println(this.mqttClient.isConnected());
        try {
            mqttClient.disconnect();
            log.debug("disconnected..");
        } catch (MqttException e) {
            log.debug("erro occured while disconneting.."+e.getCause());
        }
    }
Prabjot Singh
  • 4,491
  • 8
  • 31
  • 51

1 Answers1

0

There are two possibilities how to solve this issue:

  • Write a MQTT client that subscribes to all topics using a wildcard (# in MQTT)
  • Write a broker plugin that does the job for you, depending on the broker implementation you're using

There is a good description of how to implement both options at the HiveMQ website, also describing limitations of the first option.

Mike
  • 3,094
  • 2
  • 12
  • 5