title: MQTT subscribe client doesn't receive messages after connection
body: <p>I am trying to learn MQTT and have been playing around with it.  I've written a client for publishing and a client for subscribing (see below).</p>
  
  <p>If I run the subscribe client and then run the publish client (while subscribe is running), then everything works fine.  My subscribe client receives the messages published to the topic correctly.</p>
  
  <p>However, if I run the publish client first (ie. I publish a message to a topic) and then I run the subscribe client, I receive no messages.</p>
  
  <p>In other words, if I connect with the sub client first and then publish messages with the pub client while sub client is connected, everything works fine.  However, if I publish a message first, and then connect with my sub client, I receive no messages.  My understanding is that I should receive the messages that are present on the topic once I connect with a client and subscribe to the topic.</p>
  
  <p>I found what seems a similar issue: <a href="https://stackoverflow.com/questions/23390900/cannot-receive-already-published-messages-to-subscribed-topic-on-mqtt-paho">Cannot receive already published messages to subscribed topic on mqtt paho</a>, although that case seems a little different.  I've tried changing different QoS setting or cleanSession flag, but that didn't resolve the issue.</p>
  
  <p>Any help would be appreciated!</p>
  
  <p>Publish Client:</p>
  
  <pre><code>public class MQTT_Client_Pub implements MqttCallback{
  
  MqttClient client;
  
  public static void main(String[] args) {
  
      new MQTT_Client_Pub().mqttPub();
  }
  
  public void mqttPub(){
      try {
          this.setConnection();
  
          // Connect
          client.connect();
  
          // Create new message
          MqttMessage message = new MqttMessage();
          message.setPayload("A single test message from b112358".getBytes());
          message.setQos(0);
  
          // Publish message to a topic
          System.out.println("Publishing a message.");
          client.publish("pahodemo/test/b112358", message);
  
          // Disconnect
          client.disconnect();
  
        } catch (MqttException e) {
          e.printStackTrace();
        } catch (Exception e){
          e.printStackTrace();
        }
  }
  
  public void setConnection(){
      // Client
      try{
          client = new MqttClient("tcp://iot.eclipse.org:1883", "mqtt_test_b112358_pub");
      } catch (MqttException e) {
          e.printStackTrace();
      }
  
      // Connection Options
      MqttConnectOptions options = new MqttConnectOptions();
  
      // Set the will
      options.setWill("pahodemo/clienterrors", "CRASHED - CONNECTION NOT CLOSED CLEANLY".getBytes(),2,true);
  
      // Set Callback
      client.setCallback(this);
  }
  
  public void deliveryComplete(IMqttDeliveryToken token) {
      System.out.println("Message delivered to the broker.");
  }
  
  public void messageArrived(String topic, MqttMessage message) throws Exception {}
  
  public void connectionLost(Throwable cause) {}
  </code></pre>
  
  <p>}</p>
  
  <p>Subscribe Client:</p>
  
  <pre><code>public class MQTT_Client_Sub implements MqttCallback{
  
  MqttClient client;
  
  public static void main(String[] args) {
  
      new MQTT_Client_Sub().mqttSub();
  
  }
  
  public void mqttSub(){
      try {
          // Set connection
          this.setConnection();
  
          // Connect
          client.connect();
  
          // Subscribe
  
          client.subscribe("pahodemo/test/b112358", 0);
          // Disconnect
          // client.disconnect();
  
        } catch (MqttException e) {
          e.printStackTrace();
        }
  }
  
  public void setConnection(){
      try {
          // Client
          client = new MqttClient("tcp://iot.eclipse.org:1883", "mqtt_test_b112358_sub");
      } catch (MqttException e) {
          e.printStackTrace();
      }
  
      // Connection Options
      MqttConnectOptions options = new MqttConnectOptions();
      options.setCleanSession(false);
  
      // Set the will
      options.setWill("pahodemo/clienterrors", "CRASHED - CONNECTION NOT CLOSED CLEANLY".getBytes(),2,true);
  
      client.setCallback(this);
  }
  
  public void deliveryComplete(IMqttDeliveryToken token) {}
  
  public void messageArrived(String topic, MqttMessage message) throws Exception {
      System.out.println("Message Arrived: " + message.getPayload() + " on tipic: " + topic.getBytes());
  }
  
  public void connectionLost(Throwable cause) {}
  </code></pre>
  
  <p>}</p>