title: Android Long Polling TCP Connection
body: <p>im trying since month to do a proper "push" without using external librarys or gcm.</p>
  
  <p>First i tried xmpp with asmack, then mosquitto with paho or ibm mqtt client and http with alarmtimer.</p>
  
  <p>Finally i ended with deacon and meteord but since it's not very good (on server side - high cpu and memory load) im trying to do a longpolling connection.</p>
  
  <p>I know that it requires a heartbeat and finally i was thinking about the Delay (300 seconds heartbeat should be enough?) and the proper way.</p>
  
  <p>Is it better (battery usage and so on) to send a Heartbeat (with 1 Byte or whatever) from server to client (delay for example 300 seconds) and setting a socket timeout on client side of 300, or is it better to do send it from the client to the server?</p>
  
  <p>Currently im using a Service which register an observer in onCreate and unregister in onDestroy.</p>
  
  <p>The Observer is observing an object which do establish a tcp socket connection in a Thread and retry it onec it's disconnects (socket timeout).</p>
  
  <p>I do also check with a broadcastreciever if the network connection changed and reconnect if needed.</p>
  
  <p>What happen when the device go into standby? Do i really need an alarmmanager or timertask to recieve OR send the packet? </p>
  
  <p>Does the Device drop the Connection onec it goes into standby? </p>
  
  <p>Currently i tried sending from server to client with a 120 second delay and even when the devices display turned of its still possible to send the heartbeat.</p>
  
  <p>But at least it looks like that the battery drain is not "acceptable".</p>
  
  <p>So.. What is the best way to do that? </p>
  
  <p>Thank you so far.</p>