14

I used eclipse MQTT for connect to MQTT server.

I can connect to server successfully but when i publish message , i got this error

Connection lost
msg : Connection lost
loc : Connection lost cause : java.io.EOFException
excep : Connection lost (32109) - java.io.EOFException

I searched for this problem . but i can't find any true answer ! some link i founded in here {here , here , here , ... }

My code :

private final String DEFAULT_HOST = "edge-mqtt.facebook.com";
private final int DEFAULT_PORT = 443;

public void connect(String protogle) throws Exception {

    this.broker =  protogle + "://"+ DEFAULT_HOST + ":" + DEFAULT_PORT;
    this.mqttClient = new MqttClient(broker,getMqttClientId() ,new MemoryPersistence() );

    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval( MQTT_KEEPALIVE);
    connOpts.setUserName( getMqttUsername() );
    connOpts.setPassword( getMqttPassword().toCharArray() );
    //connOpts.setMqttVersion( 3 );//some times it have an error
    //connOpts.setSocketFactory(SSLTunnelSocketFactory.getInstance());
    Logger.w("Connecting to broker: "+broker);
    Logger.w("isConnected:"+mqttClient.isConnected());
    try {
        IMqttToken cn = mqttClient.connectWithResult(connOpts);
        Logger.w("connected");
    }catch (MqttException me){
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        return;
    }



    this.mqttClient.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable me) {
            Logger.w("Connection lost");
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
        }

        @Override
        public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            Logger.w("message Arrived");
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            Logger.w("deliverd--------");
            try {
                MqttDeliveryToken token  = (MqttDeliveryToken) iMqttDeliveryToken;
                String h = token.getMessage().toString();
                Logger.w("deliverd message :"+h);
            } catch (MqttException me) {
                System.out.println("reason "+me.getReasonCode());
                System.out.println("msg "+me.getMessage());
                System.out.println("loc "+me.getLocalizedMessage());
                System.out.println("cause "+me.getCause());
                System.out.println("excep "+me);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



}

And publish method :

private void publish(String topic , String payload , int qosLevel) throws Exception {
    Logger.w("start publishing :");
    //payload = Helper.zlib_encode(payload);
    topic = mapTopic(topic);

    MqttMessage message = new MqttMessage();
    message.setPayload(payload.getBytes("UTF-8") );
    message.setQos(0);

    mqttClient.publish(topic , message);
    Logger.w("publised------------------");
}

output :

 Connecting to broker: ssl://edge-mqtt.facebook.com:443  
 isConnected:false  
 connected  
 start publishing :  
 deliverd--------  
 publised------------------  
 deliverd message : //my message
 Connection lost
 msg : Connection lost
 loc : Connection lost
 cause : java.io.EOFException
 excep : Connection lost (32109) - java.io.EOFException

Eclipse paho log :

 ============== Connection options ==============
 CleanSession                :  true
 SocketFactory               :  sun.security.ssl.SSLSocketFactoryImpl@6c010ee9
 MqttVersion                 :  3
 KeepAliveInterval           :  60
 ConTimeout                  :  30
 UserName                    :  . . . 
 SSLProperties               :  null
 WillDestination             :  null
 ==========================================
 2017-10-19 09:42:02,182 INFO  [MQTT Call: Bahram091547759    ] [MqttConnectionResultHandler   ]  - insta connected
 2017-10-19 09:42:02,187 INFO  [JavaFX Application Thread     ] [MqttEventHandler              ]  - About to resubscribe to all requested topics
 2017-10-19 09:42:08,559 INFO  [JavaFX Application Thread     ] [MqttAsyncConnection           ]  - Publishing message on topic "k.,".  Payload size = "3"
 2017-10-19 09:42:08,739 ERROR [MQTT Rec: Bahram091547759     ] [MqttCallbackHandler           ]  - Connection insta lost
 Connection lost (32109) - java.io.EOFException
     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java: 146)
     at java.lang.Thread.run(Unknown Source)
 Caused by: java.io.EOFException
     at java.io.DataInputStream.readByte(Unknown Source)
     at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMess age(MqttInputStream.java:65)
     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
     ... 1 more
MrNadimi
  • 1,424
  • 4
  • 18
  • 33
  • Do you have access to the logs from the broker to see why it closed the connection? My first guess would be that you are not authorised to publish to the topic you are using. – hardillb Oct 17 '17 at 11:38
  • @hardillb no i don't check the log . can you tel me where is the log ? and how can i check this ? – MrNadimi Oct 17 '17 at 11:52
  • Not without knowing a lot more information such as what broker you are using and how you installed it. (edit the question to add these details) – hardillb Oct 17 '17 at 11:58
  • @hardillb Eclipse paho log added – MrNadimi Oct 19 '17 at 06:19
  • Still doesn't help, the connection was closed by the broker. The only way to know why is to see the broker logs, which since they belong to Facebook is unlikely to happen. As I said, most likely situation is the user is not authorised to publish to the topic – hardillb Oct 19 '17 at 06:23
  • i can connect with this username and password in php with this library https://packagist.org/packages/binsoul/net-mqtt-client-react but when i implement this mqtt in java . i got error ! – MrNadimi Oct 19 '17 at 06:43
  • But can you publish to the exact same topic? The other option is that there is 2 clients with the same client-id but that is less likely as it seams to only fail after a publish – hardillb Oct 19 '17 at 06:45
  • yeah . i can publish with same topic . i tested with same and diffrent client id ! but my problem not sloved – MrNadimi Oct 19 '17 at 06:49
  • and i check it with https://github.com/eclipse/paho.mqtt-spy . i got problem again and again – MrNadimi Oct 19 '17 at 06:51
  • So as I said, without the broker logs, we can not answer this – hardillb Oct 19 '17 at 06:53
  • would you tel me how can i get the broker log to update this question ? if you mean what is the mqtt type of server ? i think it is mqtt mosquitto – MrNadimi Oct 19 '17 at 06:55
  • I answered that already, Ask Facebook for the logs. And no it's not mosquitto, Facebook have written their own broker implementation that may not be totally standard. – hardillb Oct 19 '17 at 06:57
  • it's mosquitto . please check the license https://mosquitto.org/wp-content/uploads/2011/08/image.png and https://www.facebook.com/notes/facebook-engineering/building-facebook-messenger/10150259350998920/ – MrNadimi Oct 19 '17 at 07:00
  • That does not say the broker is mosquitto, it implies the client uses the mosquitto client libraries, it is well known that Facebook book have written their own broker. Also it doesn't matter what broker it is if you can't get the logs – hardillb Oct 19 '17 at 07:04
  • @hardillb hi again . i can login with this mqtt broker by https://github.com/binsoul/net-mqtt-client-react . do you know how can i find the same mqtt library for java ? – MrNadimi Oct 23 '17 at 11:30
  • Got the same issue, then I figured I was using same CLIENT ID in two mobile devices. removing which fixed it. – Artist404 Jan 19 '18 at 08:57

4 Answers4

20

I had the same exception and fixed it by ensuring a unique client ID was generated (with MqttAsyncClient.generateClientId()), as mentioned here: https://github.com/eclipse/paho.mqtt.java/issues/207#issuecomment-338246879

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
wrapperapps
  • 937
  • 2
  • 18
  • 30
  • Just had the same behaviour, the server closed the connection because there where several clients with the same id. – Marvin Dec 31 '22 at 10:53
1

The javadoc for connectWithResult recommends to call setCallback(MqttCallback) prior to connecting in order that messages destined for the client can be accepted as soon as the client is connected.

Try moving the mqttClient.setCallback call up in your source code.

Also try running your program with java -Djavax.net.debug=all

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
0

Try to reduce the keep-alive time to 15 sec and setConnectionTimeout to 30

  connOpts.setKeepAliveInterval(15);
  connOpts.setConnectionTimeout(30);
Abhay Pratap
  • 1,886
  • 11
  • 15
  • 1
    In my case also, If I keepalive as 20 seconds,im not getting EOF exception. Any idea why it is working? – kavie Oct 12 '20 at 13:51
0

Recently I've worked with this. I fronted the same issue: a 32109 error just after a successful connection.

It's a reported issue on Paho's repository.

I updated my library from:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.1'

to:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.2'

as the repository says.

And that fixes it.

markomoreno
  • 318
  • 3
  • 5