There was an error while loading. Please reload this page.
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
Text
Command
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.
Procedure
sendProcedure :: Engine -> Procedure a -> IO a
Procedure is an Applicative Functor, and a Command can be promoted into a Procedure.
Applicative
Functor
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.
middleware
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!