Search code examples
javaspringclojure

How to connect to a locally running REPL via Leiningen?


I have a Spring Boot application in which I create a REPL as shown in this answer.

That is, I have the following code:

@Service
public class ClojureRepl {
    private final Logger logger = LoggerFactory.getLogger("CLOJURE");

    @PostConstruct
    public void init() {
        final IFn require = Clojure.var("clojure.core", "require");
        require.invoke(Clojure.read(MAIN_CLOJURE_NAMESPACE));
        Clojure.var("clojure.core.server", "start-server").invoke(
                Clojure.read("{:port 5555 :name spring-repl :accept clojure.core.server/repl}")
        );
    }

    @PreDestroy
    public void destroy() {
        Clojure.var("clojure.core.server", "stop-server").invoke(
                Clojure.read("{:name spring-repl}")
        );
    }
}

When I start the application and enter nc localhost 5555 on the command line, I am able to connect to that REPL.

I want to try out using nRepl as a client.

So, I tried entering

lein repl :connect 127.0.0.1:5555

It only outputs

Connecting to nREPL at 127.0.0.1:5555

and then nothing happens.

How can I connect to the server REPL using lein repl?


Solution

  • The built-in Clojure server in clojure.core.repl just starts a socket server with a plain text protocol, this is why it works well with telnet.

    Leiningen uses the nREPL protocol. If you want to use it, try opening the server using the nREPL library with nrepl.server/start-server.