-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Andy Gill edited this page Jan 15, 2018
·
5 revisions
JavaScript Bridge is an easy way of calling JavaScript from Haskell, using web-sockets as the underlying transport mechanism.
JavaScript Bridge runs JavaScript fragments. The basic
Haskell method is sendCommand
,
which sends a Text
based Command
to a JavaScript Engine
.
sendCommand :: Engine -> Command -> IO ()
There are also ways synchronously sending a Procedure
,
that is a command that returns a value, and waiting the
the result.
sendProcedure :: Engine -> Procedure a -> IO a
Procedure
is an Applicative
Functor
, and a Command
can
be promoted into a Procedure
.
Finally, there is a way of Listening for events, etc.
sendListener :: Engine -> Listener a -> (a -> IO ()) -> IO ()
First, use a middleware
to setup the (Haskell) server.
import qualified Network.JavaScript as JS
...
scotty 3000 $ do
middleware $ JS.start print example
Next, include the following fragment in your HTML code, replacing localhost with your web address.
<script>
jsb = new WebSocket('ws://localhost:3000/');
jsb.onmessage = function(evt){ eval(evt.data);};
</script>
That's it!