Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting the ClojureScript browser REPL

I'm trying to start a ClojureScript REPL in a browser. Ideally I'd like not to use Austin yet: I first want to get the simplest REPL possible working.

I tried following various tutorials and so far I didn't manage to make it work. Basically I don't understand what people mean when they say: "Start the ClojureScript browser REPL".

So far I did:

lein new mies hellow

And:

lein cljsbuild auto hellow

And here's my core.js file:

(ns hellow.core
  (:require [clojure.browser.repl :as repl]))

(enable-console-print!)

(println "Hello world!")    
(. js/console (log "Hello again"))    
(repl/connect "http://localhost:9000/repl")    
(. js/console (log "Ah, this prints too"))

When I open my index.html (which calls core.js), I can see in Chrome's developer tools's JavaScript console that everything is correctly logged.

However the (repl/connect ...) obviously fails. Here's what Chrome's developer tools shows;

Failed to load resource http://localhost:9000/repl?xpc=%7B%22cn%22%...

In nearly every forum post / blog entry / tutorial about ClojureScript I read about that subject, I stumble upon a line saying: "Start the browser REPL" or something similar.

How do I start the browser REPL? Is this something that must be done before loading index.html from the browser?

Can I start this browser REPL from Emacs?

How can I test that the browser REPL is working without loading index.html / core.js?

How can I then verify, after index.html / core.js is loaded in the browser, that the (repl/connect ...) inside core.js did work and is actually connected to the REPL?

like image 577
Cedric Martin Avatar asked Nov 27 '25 05:11

Cedric Martin


1 Answers

The simplest and most basic way to start the repl server (you are already including the client code) is doing

$ lein trampoline cljsbuild repl-listen
Running ClojureScript REPL, listening on port 9000.
To quit, type: :cljs/quit
ClojureScript:cljs.user>

At that point, if you try to eval any expression like (+ 1 2) for example, you will see the repl hangs, because it has no environments to eval.

That's when you go to your index.html (remember to serve it from a web server, or the connection will fail, don't use the file:/// urls) and open it on a browser.

The connection to http://localhost:9000/repl?xpc=... should work just fine, and the repl should unblock and print 3. From then on, any commands will be executed in the browser environment.

I like to try (js/alert "hi") to see if the repl is connected, if it works it will alert hi in the browser window.

Remember that the browser window is your execution environment, so if you refresh it, you will lose the runtime values and they won't be accessible from the repl if you don't define them again.

With cljsbuild there is also: (lein cljsbuild help)

repl-listen
  Run a REPL that will listen for incoming connections. (the one I used above)
repl-launch
  Run a REPL and launch a custom command to connect to it.
repl-rhino
  Run a Rhino-based REPL (JVM based JS execution environment, no browser needed).

Also, for the barebones repl, it is useful to use it with rlwrap to get readline shortcuts (Ctrl+a, etc) and repl history among other things:

$ rlwrap lein trampoline cljsbuild repl-listen

I hope all this helps.

like image 93
Joaquin Avatar answered Nov 30 '25 00:11

Joaquin