Weasel uses WebSockets to communicate between a ClojureScript REPL, which is typically hosted on nREPL using piggieback, and an environment which can execute compiled ClojureScript, which can be a web browser or any JavaScript environment that supports the WebSocket APIs.
Starting with Weasel 0.6.0
, you will need at least ClojureScript
version 0.0-2850
and Piggieback version 0.1.4
. You may want to
use Piggieback version 0.1.5
or newer, however, as it fixes a number
of bugs related to the new ClojureScript REPL APIs.
CrossPageChannel
transport, which is used in the
standard ClojureScript browser REPL and Austin. (see:
cemerick/austin#17, cemerick/austin#49,
cemerick/austin#49)CrossPageChannel
may be unfeasible due to restrictions on or
unavailability of <iframe>
elements. Weasel allows the
ClojureScript developer to still enjoy the benefits of REPL driven
development in these exotic domains.Weasel is intended to be used with Chas Emerick's
piggieback nREPL middleware. Once you've set that up,
add Weasel as a dependency to project.clj
:
[weasel "0.6.0"]
Start up lein repl
and piggieback the Weasel REPL environment onto
the nREPL session, optionally specifying a port (defaults to 9001) and
an address to bind to (defaults to "127.0.0.1").
user> (require 'weasel.repl.websocket)
nil
user> (cemerick.piggieback/cljs-repl
:repl-env (weasel.repl.websocket/repl-env
:ip "0.0.0.0" :port 9001))
<< started Weasel server on ws://0.0.0.0:9001 >>
Type `:cljs/quit` to stop the ClojureScript REPL
nil
+Note for Piggieback >= 0.2.0:
The function signature for
piggieback/cljs-repl
changed in version0.2.0
to accept a repl-env as the first argument, instead of as a keyword argument:user> (cemerick.piggieback/cljs-repl (weasel.repl.websocket/repl-env :ip "0.0.0.0" :port 9001))
In your project's ClojureScript source, require the Weasel client namespace and connect to the REPL.
(ns my.cljs.core
(:require [weasel.repl :as repl]))
(when-not (repl/alive?)
(repl/connect "ws://localhost:9001"))
You may optionally specify the following:
:verbose ; boolean, defaults to true
:print ; :repl to print only to the repl,
; :console to print only to the console
; #{:repl :console} to print to both
; or any variadic function to handle printing differently.
; defaults to :repl
:on-open, :on-error, :on-close ; fns for handling websocket lifecycle events.
; default for all is nil
Connecting with options:
(repl/connect "ws://localhost:9001"
:verbose true
:print #{:repl :console}
:on-error #(print "Error! " %))
Load the page in your WebSocket-enabled environment (probably a browser) and start evaluating ClojureScript at the REPL:
cljs.user> (= (js/Number. 34) (js/Number. 34))
false
cljs.user> (do (js/alert "Hello world!") 42)
42
Note that unless a client is connected to the WebSocket channel, evaluation will fail:
cljs.user> (+ 5 10)
java.io.IOException: No client connected to Websocket
nil
Only a single client can be connected to the REPL at once. Attempting to connect to an occupied REPL server will throw an exception in the client.
An example project is included in the weasel-example
subdirectory of
this project.
If you have any feedback or issues to report, feel free to open an issue on GitHub.
A little treat for reading the whole README!
Can you improve this documentation? These fine people already did:
Tom Jakubowski, Matt Huebert, Tom & Bozhidar BatsovEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close