title: How to architect a django app that requires bidirectional communication with the client
body: <p>I have a webapp (django/node.js) and a client (not a browser, a console app that uses requests package) that are connected through the internet (completely seperate networks). It is expected that the client will not always be alive, will be connected to the internet over an unreliable internet connection.</p>
  
  <p>I need some advice on architecting the application, and the system should do the following:</p>
  
  <ol>
  <li><p>I need to be able to push data over to the client from the server, and when this happens, the client will update its local MSSQL db based on the data received, and acknowledge to the server that the data update was successfull (i.e. bidirectional communication)</p></li>
  <li><p>The server could also ask if the client is alive.... the client will respond if it alive, and this is used as a heartbeat to monitor the state client. If it is alive, the server could ask the client to initiate a long process (for eg. start backup of its db and upload). If the client does not respond, there is a sort of timeout, and an email will be sent out saying that the server cannot reach the client.</p></li>
  </ol>
  
  <p>Here are some constraints:</p>
  
  <ul>
  <li><p>I have tried server side events to to push to client, and the client posts to url its response. The first issue was that it the client would consume cycles on the processor just wating for an event (same issue even if I try with a browser like chrome or firefox - while it is waiting, the percentage of processor use jumps up be around 8% or so). The other issue is that code to kick of a ARE_YOU_ALIVE from the server will stall, a  client will post to a different location/view, and I have to grab that information. Looks like a disaster waiting to happen. So this approach is abandoned.</p></li>
  <li><p>I really do not want to use tornado, twisted etc. Really do not want to invest time in this for now. Most of my app is working well, and do not want to reinvent the wheel for this small feature.</p></li>
  <li><p>I am convinced that the right approach involves node.js and socketio, and would like to use them. At the same time, I am no JS ninja, and would like to have django do the heavy lifting. I like how <a href="http://maxburstein.com/blog/realtime-django-using-nodejs-and-socketio/" rel="nofollow">this article solves the problem</a>, and has minimal node.js, and lets django take care of the logic. However, the issue seems to be that the <code>node_api(request)</code> (which is where the client will post its response to) will not know which ARE_YOU_ALIVE request the client is responding to. I really needs some thing simple like:
  <code><pre>
  client_alive = Get_client_status(client)
  if client_alive:
    send_command_to_client(command['INIT_BACKUP']
    response = Get_client_status(client)<br>
    block until response.state == "BACKUP_COMPLETE":
         response = Get_client_status(client)
    # need to handle timeout if backup is stuck and will not complete for some reason<br>
  else:  timeout?
     send_email_client_dead()
  </pre></code>
  Is there a known pattern on how people typically handle this situation? I feel like I am doing something new, but have not come across any docs on how this is done. I would appreciate any guidance.</p></li>
  </ul>