jakub holý

building the right thing, building it right, fast

How to use Clojure 1.10 pREPL to connect to a remote server (WIP)


Clojure 1.10 includes a new, program-friendly REPL or prepl (pronounced as “preppy,” not p-repl). However there is still very little documentation about it, though it is reportedly in making (it is alpha, after all). Here I want to demonstrate how to start it and how to connect to it in a primitive way (I hope to improve the user experience of the client eventually).

Update 22/3: Check out O. Caldwell’s Clojure socket prepl cookbook.

Start the server

(Thanks to Oliver Caldwell’ Vim Conjure plugin docs for this!)

To start the prepl server from the command line (I normally need only the first one):

clj -J-Dclojure.server.jvm="{:port 5555 :accept clojure.core.server/io-prepl}" \
    -J-Dclojure.server.node="{:port 5556 :accept cljs.server.node/prepl}" \
    -J-Dclojure.server.browser="{:port 5557 :accept cljs.server.browser/prepl}"

Or from Clojure REPL:

(require '[clojure.core.server :as server])
(server/start-server {:accept 'clojure.core.server/io-prepl
                        :address ""
                        :port 5555
                        :name "my prepl"})

Connect with the client

;; shell$ clj
user=> (require '[clojure.core.server :as server])
user=> (server/remote-prepl "" 5555 *in* println)
(+ 1 2) ;; input
{:tag :ret, :val 3, :ns user, :ms 20, :form (+ 1 2)} ;; output

(println "hi" "there") ;; input
{:tag :out, :val hi there
} ;; output
{:tag :ret, :val nil, :ns user, :ms 4, :form (println "hi" "there")}  ;; output

We would likely want to show a nice user prompt and display nicely the output instead of println-ing the EDN returned from the server.

More about prepl

Check out the clojure.core.server api docs and the prepl schema:

prepl schema