Getting started with nREPL server and REPL-y client
01 Mar 2024
If you are using Clojure, you might be interested in nREPL which lets you connect a REPL terminal to a running Clojure program. In the following howto I am setting up a small nREPL demo using the nREPL server and the REPL-y nREPL client.
First I set up aliases for the server and client in $HOME/.clojure/deps.edn as follows:
Now I need a small demo program to test things out. First I create $HOME/Documents/repltest/deps.edn which just specifies the Clojure version.
The following program then displays a counter which gets increased once per second. Furthermore it starts an nREPL server on port 7888. The program goes into the file $HOME/Documents/repltest/src/repltest/core.clj.
Now one can run the program using clj -M:nrepl -m repltest.core. The program will print out consecutive numbers as follows:
Now you need to open a second terminal for the nREPL client. You run the network client using clojure -M:reply. The important thing which took me some time to find out is that you need to then switch to your applications namespace as follows:
Now you can easily access the variables of the main program:
You can also modify the value while the main program is still running:
You should see the counter decrease in the application’s output.
You can even redefine the display methods using the nREPL client. I.e. you can do interactive development.
The program output will now be modified as follows:
See github.com/wedesoft/repltest for the demo code.
Enjoy!