shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
victorb 2020-12-29T10:55:11.176900Z

The following is in the docs: > If you want to switch to a CLJS REPL this may require additional setup in the tool you used to start the server in. Since `lein` will default to using nREPL it will require configuring additional nREPL `:middleware`. When using `clj` you are good to go since it doesn’t use nREPL. I'm trying to manually connect to the shadowcljs nrepl and run (shadow/repl :build-id) to create a cljs repl, but I'm not getting it to work. I guess it's supposed to change the ns for me, but getting nothing. Works when running via lein repl :connect , changes the ns properly and drops my into a cljs repl, but no dice when manually connecting via sockets. Any ideas what I'm missing?

thheller 2020-12-29T11:14:08.177200Z

@victorbjelkholm429 you are connecting to what? the shadow-cljs nrepl server? the lein one?

thheller 2020-12-29T11:14:51.177700Z

and what are you connecting with? if you are coding this manually you need to properly manage the nrepl session stuff

victorb 2020-12-29T12:14:27.180100Z

@thheller yeah, connecting directly to the nrepl started by shadow-cljs. Using the lein nrepl client just as a test client, main usage is supposed to be via a Java Socket manually in Clojure code. Yeah, already using session + id, bencode and the whole rabble. Any information around what the command shadow/repl expects my nrepl session to do? I'm assuming it'll return me a ns that I should switch to, while retaining the same session, or something like that but haven't quite figured it out yet

thheller 2020-12-29T12:17:44.181400Z

I can't remember either. I think clients usually clone and then shadow/repl switches the current session to CLJS. don't know if thats supposed to return something.

thheller 2020-12-29T12:18:51.181600Z

CLJS over nrepl is horrible

victorb 2020-12-29T12:23:55.182Z

hah, it's a bit messy at times I guess, but unsure if there is any alternatives?

victorb 2020-12-29T12:23:59.182200Z

I'll continue digging, thanks!

thheller 2020-12-29T12:24:30.182600Z

don't know what you intend to do exactly

victorb 2020-12-29T12:25:31.183600Z

want to run cljs code in the browser from a clojure process programmatically, and since shadow-cljs runs as a sub-process to the clojure process, I'm using nrepl to communicate. That's the summary I guess

thheller 2020-12-29T12:26:16.184100Z

shadow.remote stuff is the easiest (if you can do websocket and transit) in my opinion but I'm quite biased 😉

victorb 2020-12-29T12:27:19.184800Z

ah, very cool, didn't know about shadow.remote! Seems to be an alternative, will take a closer look

thheller 2020-12-29T12:27:21.184900Z

socket repl is easy too if you don't care much about the output

thheller 2020-12-29T12:27:48.185300Z

you already looked at the shadow.remote stuff. its the websocket endpoint you tried a couple days ago.

thheller 2020-12-29T12:28:13.185600Z

https://github.com/thheller/shadow-cljs/blob/master/doc/remote.md its a bit out of date but the idea is the same

victorb 2020-12-29T12:28:55.186500Z

yeah, didn't look to much at it the other day as I found I could get a nREPL connection up and running 😄 But, back to it we go.

thheller 2020-12-29T12:30:18.187100Z

basically all you need to send is the {:op :hello} and then get the client id of the CLJS runtime you want to eval in

thheller 2020-12-29T12:30:23.187300Z

if you know it

{:op :cljs-eval
 :to client-id
 :input {:code "(+ 1 2)"
         :ns 'cljs.user
         :repl true}}

thheller 2020-12-29T12:30:26.187600Z

this gets you eval

thheller 2020-12-29T12:31:10.188200Z

:repl true is optional. if you don't care about *1 *2 and so on you can leave it out

victorb 2020-12-29T12:32:12.188800Z

awesome, thanks!

thheller 2020-12-29T12:32:29.189100Z

if you want to emulate "RPC" you can send :call-id 123 with the request and the server will reply with that id in the message

🙏 1
mauricio.szabo 2020-12-29T15:43:44.190300Z

@victorbjelkholm429 if you want to take a look, I wrote a client to connect to shadow.remote - I use it on Chlorine / Clover plug-ins: https://github.com/mauricioszabo/repl-tooling/blob/master/src/repl_tooling/repl_client/shadow_ws.cljs

mauricio.szabo 2020-12-29T15:47:28.193Z

It's a ClojureScript package, so you can even import on your CLJS code and run right away! It does a lot more than just connecting to Shadow, but you can ignore most parts if you want (I believe that CLJS's dead code removal will handle it). Please, ping me if you think it helps, because I'm also researching on a web-based evaluator 🙂

victorb 2020-12-29T15:49:05.194200Z

@mauricio.szabo thanks a lot! In the end I went the nREPL way, so I can interact with the shadowcljs nrepl the same way I interact with clojure and babashka nrepls, less code 🙂 Figured out the issues I was seeing was my fault, I was creating a new session in the wrong place, instead of reusing an existing one, got everything to work as expected now