16

I have two, related questions about RPC call to Yesod/Snap/Servant:

  1. Say I have a function on the server, written in Haskell, compiled by GHC:

    add x y = x+y+42

How can:

  1. I call this function from the client (the client is also written in Haskell, compiled with GHCJS),
  2. execute the function on the server,
  3. return the result to the client?

Do all this in a type-safe way?

So for example, I would like to write something like this on the client:

main= putStrLn $ show $ add 2 3

Such that the add function gets executed on the server.

What is the simplest way of doing this using Yesod/Snap/Servant on the server and GHCJS on the client?

2) Similarly, I would like to write a "function" : getPhaseOfMoon :: IO Phase which runs on the server and I would like to call getPhaseOfMoon from the client in a type-safe way.

So that I can write this on the client:

main= do 
   p<-getPhaseOfMoon
   putStrLn $ show p
   return ()

Which

  1. goes to the server
  2. the server consults the phase of the moon
  3. the server returns Phase to the client
  4. the client displays the phase of the moon in the JavaScript Console.

Question

How can I do this with Yesod/Snap/Servant (server) + GHCJS (client)?

timiTao
  • 1,417
  • 3
  • 20
  • 34
jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • 4
    I'm also thinking about this recently. I'm using GHCJS for client and on the server side, no framework is used yet. The best result I can find is Haste.App for the Haste compiler. Not sure if the idea can be transported to GHCJS because GHCJS has concurrent support. Here's the link: http://haste-lang.org/haskell14.pdf – Eric Wong Oct 09 '15 at 09:20
  • 2
    I don't know if that's what you meant here, but as a general principle, I would warn against any solution that allows an untrusted web client to run "arbitrary code" on a server. Even with Safe Haskell; we found some nasty loopholes in that over the last year. – Ørjan Johansen Oct 09 '15 at 12:10
  • Thanks for the warning , indeed that is a good point. The main goal of this question is to help people get started using GHCJS on the client and demo a simple way of communicating with Haskell backend. – jhegedus Oct 09 '15 at 15:02
  • I was told that here is tutorial on how to do this : http://ruhaskell.org/posts/web/2015/09/01/wai-rpc.html :) I need to first learn Russian. Github example: https://github.com/astynax/wai-rpc – jhegedus Oct 09 '15 at 15:06
  • 1
    This seems to be useful as well: http://haskell-servant.github.io/tutorial/#tutorial - I was told. – jhegedus Oct 09 '15 at 16:39
  • some more related links : 1) forum discussion https://groups.google.com/forum/#!msg/yesodweb/unP9SnS334o/kzzenXd8cXUJ , 2) a video : https://www.youtube.com/watch?v=Bnf7AmoQ6lc 3) good paper : http://www.andres-loeh.de/Servant/servant-wgp.pdf – jhegedus Oct 09 '15 at 20:16
  • This link comes very close to an answer : https://github.com/arianvp/ghcjs-servant-client and this : https://github.com/plow-technologies/ghcjs-servant-client, many thanks to friendly folks at #servant for the tips – jhegedus Oct 09 '15 at 20:28
  • .... and this video by one of Servant's inventors : https://www.youtube.com/watch?v=snOBI8PcbMQ – jhegedus Oct 09 '15 at 20:36

0 Answers0