Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] mqtt publisher topic

Dear sir



               I already have  a broker running on my server and have subscribed few topics. If a new client wants to send his data to my server through my mqtt broker. Is there a possibility to obtain his ip address , publisher id ( and his new topic which he s gonna publish). Right now i am sending a sample program in which , if the publisher sends his data through the already subscribed topic then messages are received. If a new client connect to my broker with a new unsubscribed topic . how can i identify the topic name and publisher id, ip address. The subscriber program is as follows

package Testing;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule;

public class subs implements MqttCallback
{

private static final String SERVER_URL = "tcp://localhost:1883";
private static final String CLIENT_ID = "myclient";

public String topic="com/newregistration";

private static MqttClient mqttClient;



public static void main(String[] args)throws Exception
{
subs obj=new subs();
obj.test();
}


public void test() throws Exception {
try {


mqttClient = new MqttClient(SERVER_URL, CLIENT_ID);
mqttClient.connect();

mqttClient.setCallback((MqttCallback) this);

System.out.println("connected with broker");
//mqttTopic.publish(message);


mqttClient.subscribe(topic);
}
catch (MqttException e) {
e.printStackTrace();
}
}

     
public void connectionLost(Throwable cause) {
System.out.println("Connection Exist. \nCause: " + cause);
}


public void messageArrived(MqttTopic topic, MqttMessage message)
throws Exception {
System.out.println("Message arrived From The Topic:\t"+topic.toString() +" \nMessage: " + message.toString());
}


public void deliveryComplete(MqttDeliveryToken token) {
System.out.println("Delivery complete. \nToken: " + token.toString());
}


}

The publisher program is as follows 


package Testing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;


import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;

public class ClientPublisher {

private static final String SERVER_URI = "tcp://localhost:1883";
private static final String 
CLIENT_ID = "pub";
private MqttClient mqttClient;
public static String topic="hello";

public static void main(String args[]) throws Exception {
new ClientPublisher().doMqttTest(topic);
}


public void doMqttTest(String clienttopic) throws IOException {
try {
mqttClient = new MqttClient(SERVER_URI, CLIENT_ID);
mqttClient.connect();
} catch (MqttException e) {
e.printStackTrace();
}

while (true) {
//Date date = Calendar.getInstance().getTime();
String date="hey";
//System.out.println(date.toString());
try {
// Get an instance of the topic
MqttTopic topic = mqttClient.getTopic(clienttopic);
MqttMessage message = new MqttMessage(date.toString().getBytes());
MqttDeliveryToken token = topic.publish(message);
//System.out.println(topi);
// Wait until the message has been delivered to the server
token.waitForCompletion();
Thread.sleep(50 * 1000);
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


Regards 
Kannadhasan
 

Back to the top