liveBook · Manning

Clojure.core.server

As explained in Clojure: The Essential Reference

clojure.core.server is the Clojure namespace implementing server-socket functionality.

clojure.core.server exposes the Clojure environment through sockets, letting remote processes or people send Clojure requests and receive evaluated results. Its default service is a socket-based variant of the console REPL. section 22.7

Configure a server with a map passed to start-server:

  • :port and :name are required; :address defaults to 127.0.0.1. section 22.7
  • :accept selects the fully qualified request handler, while :args supplies optional handler arguments. section 22.7
  • :bind-err controls whether standard error is sent through the socket, and daemon options control JVM and client-thread lifetime. section 22.7

Start and use a remote REPL

Require the namespace and start a named server with (server/start-server {:name "repl1" :port 8787 :accept clojure.core.server/repl}). A client can connect with telnet 127.0.0.1 8788, evaluate (+ 1 1), and receive 2. Stop one server with (server/stop-server "repl2") or stop all servers with (server/stop-servers). section 22.7

(require '[clojure.core.server :as server]) ; #1

(server/start-server ; #2
  {:name "repl1" :port 8787 :accept clojure.core.server/repl})
;; #object["ServerSocket[addr=localhost/127.0.0.1,localport=8787]"]

Inspect the minimal start-server form and its returned ServerSocket representation. section 22.7

How Clojure: The Essential Reference covers it

How deep it goes
Defines startup options, handlers, streams, daemon behavior, client interaction, and shutdown, with limited implementation detail.
Examples
Uses start-server, telnet evaluation, stop-server, stop-servers, and a command-line Clojure 1.8.0 server property.
What you can do after
Operate/configure a named socket REPL, connect remotely, evaluate expressions, and stop individual or all servers.
How it connects
Connects socket servers with the REPL, Clojure evaluation, standard streams, and daemon threads.

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.