Fork me on GitHub
#beginners
<
2023-07-14
>
Δημήτριος Φκιαράς11:07:11

Hello, I am trying to built an htlm/javascript UI to control my small Clojure App. I thought to do so by sending commands from UI to the App via websockets. This is my code on the clojure side (the handler function):

(defn ws-handler [request]
  #_{:clj-kondo/ignore [:unresolved-symbol]}
  (s/with-channel request channel
    (connect! channel)
    (s/on-close channel (partial disconnect! channel))
    (s/on-receive channel #(receive-command %))))
My receive-command function gets the message from the client (UI side). I know so because I put it to spit the message in a text file. BUT when I for example to change the receive-command function to print, or println or do something else with the incoming message I am not able to do so. Why is that?

delaguardo11:07:17

most likely because of the way how you inject this handler into your router. Could you share that part as well?

❤️ 2
Δημήτριος Φκιαράς11:07:29

I am pretty new to this. I actually don’t have a router… This is all my code:

(:require '[org.httpkit.server :as s])

(defonce channels (atom #{}))

(defn receive-command [command]
  (println command))

(defn connect! [channel]
  (println "channel open")
  (swap! channels conj channel))

(defn disconnect! [channel status]
  (println "channel closed:" status)
  (swap! channels #(remove #{channel} %)))

(defn ws-handler [request]
  #_{:clj-kondo/ignore [:unresolved-symbol]}
  (s/with-channel request channel
    (connect! channel)
    (s/on-close channel (partial disconnect! channel))
    (s/on-receive channel #(receive-command %))))

(defn notify-clients [msg]
  (doseq [channel @channels]
    (s/send! channel (str msg))))

(defn run-server [port]
  (s/run-server ws-handler {:port port}))

delaguardo12:07:01

good, thanks. When you start your server s/run-server function takes a value from the var ws-handler and starts using this function as a main requests handler. This should help: (s/run-server #'ws-handler {:port port}) Note #' at the beginning of handler name. The difference between ws-handler and #'ws-handler is that the second works as a pointer to some value so when you redefine the var your server will be able to pick up that change.

❤️ 2
Δημήτριος Φκιαράς12:07:05

This is very interesting. I will study about #’. I just tried it and still cannot print to repl. Note that I am using VS Code and Calva.

delaguardo12:07:58

my bad, it is not about ws-handler function but about receive-command

(defn ws-handler [request]
  #_{:clj-kondo/ignore [:unresolved-symbol]}
  (s/with-channel request channel
    (connect! channel)
    (s/on-close channel (partial disconnect! channel))
    (s/on-receive channel #'receive-command)))

;; run-server should stay the same
(defn run-server [port]
  (s/run-server ws-handler {:port port}))

Δημήτριος Φκιαράς12:07:35

Sorry, still no success! :thinking_face:

delaguardo12:07:57

hm... I reproduce the fix locally

delaguardo12:07:21

did you restart the server after the change?

Δημήτριος Φκιαράς12:07:16

Yes. Can you send the whole code to check, please?

delaguardo12:07:10

I used repl, sorry. the only change was in the last snippet I sent to the thread

Δημήτριος Φκιαράς12:07:55

Ok. Thanks, I will investigate. What version of http-kit you used?

delaguardo13:07:07

latest

👍 2
Δημήτριος Φκιαράς13:07:23

I checked. It works on the repl. Doesn’t work with Calva repl <!subteam^S03BGSAUPTQ|@calva-team>

bringe15:07:14

In certain situations standard output may not show up in the Calva output window, but instead in the terminal where the repl is running. Did you check your terminal for the printed output?

👍 4
Δημήτριος Φκιαράς09:07:48

Thanks, unfortunately I don’t know how to do that.

pez09:07:25

How did you start the REPL?

pez14:07:45

Then you’ll find the process output in the Terminal pane. The name of the terminal starts with “Calva Jack-in”.

Δημήτριος Φκιαράς12:07:06

But I don’t see anything getting printed there, when I evaluate some form.

bringe20:07:47

I'm not sure what's going on but if you can provide a small project in which we can reproduce the issue that would be helpful.