This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-14
Channels
- # announcements (4)
- # babashka (32)
- # beginners (24)
- # calva (3)
- # cider (37)
- # clj-commons (8)
- # clj-kondo (6)
- # clojure (51)
- # clojure-denver (7)
- # clojure-europe (48)
- # clojure-nl (1)
- # clojure-norway (2)
- # clojure-uk (2)
- # clojurescript (12)
- # core-async (10)
- # cursive (9)
- # datomic (42)
- # hyperfiddle (25)
- # jobs (4)
- # juxt (2)
- # lsp (10)
- # malli (11)
- # off-topic (32)
- # polylith (6)
- # practicalli (1)
- # react (2)
- # releases (1)
- # remote-jobs (2)
- # spacemacs (4)
- # xtdb (4)
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?most likely because of the way how you inject this handler into your router. Could you share that part as well?
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}))
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.
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.
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}))
Sorry, still no success! :thinking_face:
hm... I reproduce the fix locally
did you restart the server after the change?
Yes. Can you send the whole code to check, please?
I used repl, sorry. the only change was in the last snippet I sent to the thread
Ok. Thanks, I will investigate. What version of http-kit you used?
I checked. It works on the repl. Doesn’t work with Calva repl <!subteam^S03BGSAUPTQ|@calva-team>
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?
Thanks, unfortunately I don’t know how to do that.
Via calva
Command palette of vs code
Then you’ll find the process output in the Terminal pane. The name of the terminal starts with “Calva Jack-in”.
This is what it looks like.
But I don’t see anything getting printed there, when I evaluate some form.