title: Type safe RPC call example from GHCJS client to Yesod/Snap/Servant
body: <p>I have two, related questions about RPC call to <code>Yesod/Snap/Servant</code>:</p>
  
  <ol>
  <li><p>Say I have a function on the server, written in Haskell, compiled by GHC:</p>
  
  <p><code>add x y = x+y+42</code></p></li>
  </ol>
  
  <p>How can:</p>
  
  <ol>
  <li>I call this function from the client (the client is also written in Haskell, compiled with <code>GHCJS</code>),</li>
  <li>execute the function on the server,</li>
  <li>return the result to the client? </li>
  </ol>
  
  <p>Do all this in a type-safe way?</p>
  
  <p>So for example, I would like to write something like this on the client:</p>
  
  <pre><code>main= putStrLn $ show $ add 2 3
  </code></pre>
  
  <p>Such that the add function gets <strong>executed</strong> on the <strong>server.</strong></p>
  
  <p>What is the simplest way of doing this using <code>Yesod/Snap/Servant</code> on the server and GHCJS on the client?</p>
  
  <p>2) <strong>Similarly</strong>, I would like to write a "function" :
  <code>getPhaseOfMoon :: IO Phase</code> which runs on the server and I would like to call <code>getPhaseOfMoon</code> from the client in a type-safe way.</p>
  
  <p>So that I can write this on the client:</p>
  
  <pre><code>main= do 
     p&lt;-getPhaseOfMoon
     putStrLn $ show p
     return ()
  </code></pre>
  
  <h3>Which</h3>
  
  <ol>
  <li>goes to the server </li>
  <li>the server consults the phase of the moon </li>
  <li>the server returns <code>Phase</code> to the client</li>
  <li>the client displays the phase of the moon in the JavaScript Console. </li>
  </ol>
  
  <h1>Question</h1>
  
  <p>How can I do this with <code>Yesod/Snap/Servant</code> (server) + GHCJS (client)?</p>