суббота, 22 сентября 2018 г.

clojure websocket client example

This is my deps.edn

1) Start REPL with necessary deps.

clojure -R:1.10:repl:add-lib:test:debug-tools:rebel:xml-bind -m nrepl.cmdline -p 7888 -i -m "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"
nREPL server started on port 7888 on host 0:0:0:0:0:0:0:0 - nrepl://0:0:0:0:0:0:0:0:7888
nREPL 0.4.5
Clojure 1.10.0-alpha8
Java HotSpot(TM) 64-Bit Server VM 10.0.2+13
user=> (use 'clojure.tools.deps.alpha.repl)
user=> (add-lib 'stylefruits/gniazdo {:mvn/version "1.0.1"})
user=> (add-lib 'http-kit {:mvn/version "2.2.0"})

2) Create empty file (e.g a.clj), connect to REPL (port 7888)  and put following code there:

;; ------------server code --------------------

(use 'org.httpkit.server)

(defn async-handler [ring-request]
  ;; unified API for WebSocket and HTTP long polling/streaming
  (with-channel ring-request channel    ; get the channel
    (if (websocket? channel)            ; if you want to distinguish them
      (on-receive channel (fn [data]     ; two way communication
                            (send! channel data)))
      (send! channel {:status  200
                      :headers {"Content-Type" "text/plain"}
                      :body    "Long polling?"}))))

(run-server async-handler {:port 8080})

;; ------------client code --------------------

(require '[gniazdo.core :as ws])

(def socket (ws/connect "ws://localhost:8080/" :on-receive #(prn 'received %)))
(ws/send-msg socket "hello")
(ws/send-msg socket "world")

(ws/close socket)

вторник, 11 сентября 2018 г.

Run lein with cider from command line

Headless mode

lein update-in :dependencies conj \[org.clojure/tools.nrepl\ \"0.2.13\"\ \:exclusions\ \[org.clojure/clojure\]\] -- update-in :plugins conj \[refactor-nrepl\ \"2.4.0\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.18.0\"\] -- with-profile +mike-local repl :headless :host ::

Interactive mode

lein update-in :dependencies conj \[org.clojure/tools.nrepl\ \"0.2.13\"\ \:exclusions\ \[org.clojure/clojure\]\] -- update-in :plugins conj \[refactor-nrepl\ \"2.4.0\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.18.0\"\] -- with-profile +mike-local repl :start :host ::

where mike-local is my profile

Also, :port parameter can be used to run REPL on custom port.

воскресенье, 2 сентября 2018 г.

clojure cli repl rebel

First way

In order to run REPL (nREPL) server via clojure cli tools with beautiful rebel capabilities just add following section into deps.edn file:

:aliases {
                :repl {:extra-deps {cider/cider-nrepl                       {:mvn/version "0.18.0"}
                                               nrepl                                        {:mvn/version "0.4.5"}
                                               refactor-nrepl                          {:mvn/version "2.4.0"}
                                               com.bhauman/rebel-readline  {:mvn/version "0.1.4"}}
                  :main-opts  ["-e" "(require,(quote,cider-nrepl.main)),"
                               "-e" "(cider-nrepl.main/init,[\"refactor-nrepl.middleware/wrap-refactor\",\"cider.nrepl/cider-middleware\"])"
                               "-e" "(use,'rebel-readline.main),(rebel-readline.main/-main)"]}


Then just run with command: clojure -A:repl

Second way

:aliases {
 :repl {:extra-deps {cider/cider-nrepl          {:mvn/version "0.18.0"}
                               nrepl                      {:mvn/version "0.4.5"}
                               refactor-nrepl             {:mvn/version "2.4.0"}
                               com.bhauman/rebel-readline {:mvn/version "0.1.4"}}

clj -R:repl -m nrepl.cmdline -p 7888 -i -m "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"

This command runs repl without rebel capabilities. In order to enable rebel just call it:

user=> (use 'rebel-readline.main)
user=> (rebel-readline.main/-main)
[Rebel readline] Type :repl/help for online help info

Also, read the docs https://nrepl.readthedocs.io/en/latest/usage/