TerenceF
TerenceF

Reputation: 1

Message MQTT not receive in subscribe client

I'm working on MQTT protocol and I try to publish and subscribe with 2 distincts Java applications.

My first application is "Publish". I'm publish on a MQTT server a message.

My second application is "Subscribe". I'm subscribe to a topic and try to receive the message. But I never receive the message.

When I run the 2 applications, I begin with the "Subscribe" application and after I run the "Publish" application. When the "Publish" application begin, I lose my connection to the "Subscribe" application and I can't receive my message.

In the "Subscribe" application, my method messageArrived() is never called by client.setCallback(this). (See the code below).

Here is my 2 code application :

Publish Application : Class PubClient :

package publishclient;

import java.util.Random;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;



public class PubClient implements MqttCallback {

MqttClient client;
MqttConnectOptions connOpt; 

Random rand = new Random(); 
int nbRandom = 0;
int valMax =151, valMin = 40;

public PubClient() throws MqttException {
String broker = "tcp://88.177.147.17:1883"; // Adress MQTT Server
String clientId = "0bdd-4445-82f3-928f8ddb1887"; // ClientID
String topic1f = "ApplicationRio/capteur"; // Topic
int QoSserveur = 2;

        try {
            String uuid = "ac8da3c6-0bdd-4445-82f3-928f8ddb3294";

            MemoryPersistence persistence = new MemoryPersistence();   

            // Create 2 objects : client and connOpt
            client = new MqttClient(broker, clientId, persistence);
            connOpt = new MqttConnectOptions();

            connOpt.setCleanSession(true);

            client.setCallback(this);

             // Connection to MQTT server
            System.out.println("Connexion a : " + broker + " Publisher");
            client.connect(connOpt);  

            //Create random number for my message
            nbRandom = valMin + rand.nextInt(valMax-valMin);
            System.out.println("nb aleatoire = " + nbRandom);
            String messageAEnvoyer = uuid + "//" + nbRandom;
            System.out.println("Message a envoyer : " + messageAEnvoyer);

            MqttMessage message = new MqttMessage();
            message.setPayload(messageAEnvoyer.getBytes());
            message.setQos(QoSserveur);
            client.publish(topic1f, message);

        } catch(MqttException e) {
            e.printStackTrace();
        }
}

    @Override
    public void connectionLost(Throwable thrwbl) {System.out.println("Perdue connexion");}

    @Override
    public void messageArrived(String string, MqttMessage mm) throws Exception {
    System.out.println("Message recu est : "+ new String(mm.getPayload()));}

    @Override
    public void deliveryComplete(IMqttDeliveryToken imdt) {
    System.out.println("Message delivre au broker");
    }
}

The main (Publish) :

package publishclient;

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

public class PublishClient {

public static void main(String[] args) throws MqttException {     
    PubClient publieur = new PubClient();
}

The "subscribe" application : Class SubClient :

package subscribeclient;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;

public class SubClient implements MqttCallback {

MqttClient clientsub; 
MqttConnectOptions connOpt; 


public SubClient() throws MqttException{
String broker = "tcp://88.177.147.17:1883"; // Adress MQTT Server
String clientId = "0bdd-4445-82f3-928f8ddb1887"; // ClientID
String topic1f = "ApplicationRio/capteur"; // Topic
int QoSserveur = 2;

try{

            // Create 2 objects : client and connOpt
            clientsub = new MqttClient(broker, clientId);
            connOpt = new MqttConnectOptions();

            connOpt.setCleanSession(false);
            connOpt.setKeepAliveInterval(30);

            clientsub.setCallback(this);

             // Connection to MQTT Server
            System.out.println("Connexion a : " + broker + " Subscriber");
            clientsub.connect(connOpt);


            clientsub.subscribe(topic1f,QoSserveur);

            } catch(MqttException e){
            e.printStackTrace();
        }
}

    @Override
    public void connectionLost(Throwable thrwbl) {
        System.out.println("Connexion perdue");
    }

    @Override
    public void messageArrived(String string, MqttMessage message) throws Exception {
        System.out.println("Le message recu est : " + new String(message.getPayload()));  
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken imdt) {
        System.out.println("Message arrive");
    }
}

The main (Subscribe) :

package subscribeclient;

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

public class SubscribeClient {

public static void main(String[] args) throws MqttException {
    SubClient subscriber = new SubClient();
}

}

My 2 applications need to run in the same time, and I don't need to disconnect because I run applications all the time.

So, have you got an idea of why my "Subscribe Client" disconnect when I run the "Publish Client" and why I can't receive my message on my "Subscribe Message" ?

I use org.eclipse.paho.client.mqttv3-1.0.2.jar for the library for MQTT.

Upvotes: 0

Views: 2770

Answers (1)

hardillb
hardillb

Reputation: 59608

Client IDs have to be unique between ALL clients. You have used the same client ID for the publisher and subscriber so the broker will kick the subscriber off when the publisher connects.

Upvotes: 5

Related Questions