MQTT connect using java code

How do one connect to MQTT broker.losant.com 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://broker.losant.com:8883 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

Thanks

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();
		connOpts.setUserName(userName);
		connOpts.setPassword(pwd.toCharArray());
		connOpts.setCleanSession(true);

		sampleClient.connect(connOpts);

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://broker.losant.com:8883, 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