title: Setting up an R-Server that supports asynchronous communication with clients
body: <h2>In short</h2>
  
  <p>Is it possible set up an R-Server that can handle/dispatch multiple client requests in an <strong>asynchronous</strong> way? </p>
  
  <p>I guess I'm looking for some sort of socket communication. Or is there something even more efficient to let R talk to other applications? </p>
  
  <p>Now, I don't really care if the communication is eventually realized via "plain-vanilla socket communication" (something like <code>socketConnection(port=6011, server=TRUE)</code> for the server process, <code>socketConnection(host=Sys.info()["nodename"], port=6011)</code> for client process(es) in combination with <code>writeLines()</code> and <code>readLines()</code> of JSON character-strings) or something more "advanced" like using web server facilities based on HTTP requests.</p>
  
  <h2>More details</h2>
  
  <p>First of: My background with respect to those nitty-gritty informatics details is all "DIY", so things like <em>socket communication</em>, details about <em>master-server concepts</em> or <em>asynchronous communication</em> etc. are pretty new to me. So please don't bite ;-)</p>
  
  <p>I'd like one of my processes to act as an R-Server. AFAIU, R was not designed under a "multi-threading-paradigm". So I wonder how I can make my R-Server handle concurrent client requests (currently coming from a third party software that sends its requests/objects via JSON-style character strings) in an <strong>asynchronous</strong> way.</p>
  
  <p>In plain english I would like to tell my server process something like this:</p>
  
  <blockquote>
    <blockquote>
      <p>Once you get an incoming message at port xy, do what you need to do but <em>immediately</em> switch back to "responsive mode" to be able to handle the next client request and always take care that each client gets the results that is logically linked to its respective request.</p>
    </blockquote>
  </blockquote>
  
  <h2>Homework</h2>
  
  <p>I started out with something very basic:</p>
  
  <pre><code>con &lt;- socketConnection(port=as.numeric(port), server=TRUE)
  </code></pre>
  
  <p>This would make my R process become a server. It works a treat, but since <code>con</code> is my "only" connection object, the server is doomed to handle each client request sequentially: </p>
  
  <blockquote>
    <blockquote>
      <p>Read the input from the connection, do some calculations, write output back to the connection and only <strong>then</strong> handle the next client request.</p>
    </blockquote>
  </blockquote>
  
  <p>I thought about have my server process immediately dispatch everything that needs to be done to an <strong>auxiliary Rscript process</strong> so that the "main" server process is ready to take the next request. But since at the end of the day the results need to pass through <code>con</code> again (in my "main" server process), I think that doesn't really get me any points as the server process still needs to wait until the auxiliary process is finished before taking the next request. Or can I tell this auxiliary process to "directly" report back to the client request somehow?</p>
  
  <p>Even though I'm currently not aiming at turning my server into a full-grown web server, these sophisticated web server approaches seemed very promising to me at a first glance:</p>
  
  <ul>
  <li><a href="http://www.rforge.net/Rserve/" rel="noreferrer">RServe</a> (<a href="http://cran.r-project.org/web/packages/Rserve/index.html" rel="noreferrer">CRAN page</a>)</li>
  <li><a href="http://jeffreyhorner.tumblr.com/post/25035081768/rook-tutorial-at-user-2012" rel="noreferrer">Rook</a> (<a href="http://cran.r-project.org/web/packages/Rook/index.html" rel="noreferrer">CRAN page</a>)</li>
  </ul>
  
  <p>Is asynchronous client-communication possible with one of these?</p>
  
  <p>Any pointer whatsoever will be <strong>greatly</strong> appreciated!</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p><strong>I'll offer a bounty of 300 credits once the question is eligible.</strong></p>