Skip to main content

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

When sending messages between a producing application (publisher) and consuming application (subscriber) they both need to use the same name space in order to  exchange messages.  With pub sub the name space is via topic hierarchy.   For instance if an application is going to produce messages about stock price it might use a topic space of stock/<stocksymbol>  with real instances of  stock/IBM,  stock/MS, stock/google          The consuming application needs to be informed of the name space and can then subscribe to a specific stock such as stock/google or for all stock prices  using a wildcard subscription like stock/#

Based on this, If you are need an application to publish messages that you consume,
  • Ensure the publisher knows the topic that you are subscribed to
  • Subscribe to a top level topic + wild card and ensure the publisher uses the top level topic + one or more subtopics.
  • To see all messages going through a broker then subscribe to # and all (authorised) messages going through the mqtt broker will be seen by the subscriber
One of the principals of messaging is to decouple the producer from the consumer typically via an intermediate broker and a name space like a topic or a queue.  As a result of the decoupling, knowledge of the producer such as its IP address is not made available to the consumer.  If there is a need to do so then details of the producer could be flowed in the payload of the message.  

All the best
Dave


 



From:        kanna dhasan <thasanmca@xxxxxxxxx>
To:        paho-dev@xxxxxxxxxxx,
Date:        13/08/2012 06:17
Subject:        [paho-dev] mqtt publisher topic
Sent by:        paho-dev-bounces@xxxxxxxxxxx




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
 
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/paho-dev


Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 741598.
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

Back to the top