title: Heart-beating in STOMP client
body: <p>The design of my current stomp client process is as follows:</p>
  
  <ol>
  <li>Open stomp connection (sending CONNECT frame)   </li>
  <li>Subscribe to a feed (send a SUBSCRIBE frame)</li>
  <li>Do a loop to continually receive feed:</li>
  </ol>
  
  <blockquote>
  <pre><code>while (true) {
                  connection.begin("txt1");
  
                      StompFrame message = connection.receive();
                      System.out.println("message get header"+message.toString());
                      LOG.info(message.getBody());
  
                      connection.ack(message, "txt1");
  
                  connection.commit("txt1");
  
              }
  </code></pre>
  </blockquote>
  
  <p>My problem with this process is that I get  </p>
  
  <pre><code>java.net.SocketTimeoutException: Read timed out
   at java.net.SocketInputStream.socketRead0(Native Method)...
  </code></pre>
  
  <p>and I think the cause of this is mostly because the feed I am subscribed to gives information slower on certain times (as I normally get this error when the weekend comes, holidays or evenings). </p>
  
  <p>I have been reading up on this <a href="http://stomp.github.io/stomp-specification-1.1.html#Heart-beating" rel="nofollow">here</a> and I think this would help with my problem. However, I'm not so sure how to incorporate it with the current layout of my stomp client. Would I have to send a CONNECT header within Step 3?</p>
  
  <p>I am currently using activemq to create my stomp client if that helps.</p>
  
  <p>In the stomp <a href="http://stomp.github.io/stomp-specification-1.1.html#Heart-beating" rel="nofollow">spec</a> we have: </p>
  
  <blockquote>
    <p>Regarding the heart-beats themselves, any new data received over the
    network connection is an indication that the remote end is alive. In a
    given direction, if heart-beats are expected every  milliseconds:</p>
    
    <ul>
    <li>the sender MUST send new data over the network connection at least    every  milliseconds </li>
    <li>if the sender has no real STOMP frame to send,    it MUST send a single newline byte (0x0A) </li>
    <li>if, inside a time window of    at least  milliseconds, the receiver did not receive any new data,    it CAN consider the
    connection as dead</li>
    <li>because of timing    inaccuracies, the receiver SHOULD be tolerant and take into account    an error margin</li>
    </ul>
  </blockquote>
  
  <p>Would that mean my client would need to send a newline bye every n seconds?</p>