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

  1. Start the server
  2. Connect with the client
  3. More about prepl

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 "127.0.0.1"
                        :port 5555
                        :name "my prepl"})

Connect with the client

;; shell$ clj
user=> (require '[clojure.core.server :as server])
user=> (server/remote-prepl "127.0.0.1" 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


Tags: clojure


Copyright © 2024 Jakub Holý
Powered by Cryogen
Theme by KingMob