MQTT connect using java code


How do one connect to MQTT using piece of JAVA code. I have been trying to connect using java mqtt paho client as

MqttClient sampleClient = new MqttClient(broker, clientId);

where broker = ssl:// and clientID is the actual deviceId.

I get an exception with connection time out.

What am I doing wrong? Can some one post a code snippet to correctly connect in JAVA



Here is the piece of code I am trying to use which errors out

		MqttClient sampleClient = new MqttClient(broker, clientId); //, persistence);
		MqttConnectOptions connOpts = new MqttConnectOptions();



Hi @Sreyams_Jain

So, I haven’t used the Java paho MQTT client yet. But this should help:

The MQTT clientId = ‘my-device-id’,
The MQTT username = ‘my-access-key’,
The MQTT password = ‘my-access-secret’

Could you verify that you are using the correct MQTT options based on the feedback above? Also, could you try connecting using port 1883 to test?

If this doesn’t make any sense, let me know. I’d be happy to elaborate more.


I tried your suggestion. It did not work.

However, after several tries, setting the broker as ssl://, it worked. But I was trying to publish to a specific user defined topic, say XYZ. There is no error in publishing, But, When I subscribe to this topic to get the value, it does not show up. In the communication log all it shows is Authenticated ok

Not sure what is going wrong