Fork me on GitHub
#shadow-cljs
<
2020-12-29
>
victorb10:12:11

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?

thheller11:12:08

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

thheller11:12:51

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

victorb12:12:27

@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

thheller12:12:44

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.

thheller12:12:51

CLJS over nrepl is horrible

victorb12:12:55

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

victorb12:12:59

I'll continue digging, thanks!

thheller12:12:30

don't know what you intend to do exactly

victorb12:12:31

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

thheller12:12:16

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

victorb12:12:19

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

thheller12:12:21

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

thheller12:12:48

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

victorb12:12:55

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.

thheller12:12:18

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

thheller12:12:23

if you know it

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

thheller12:12:26

this gets you eval

thheller12:12:10

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

victorb12:12:12

awesome, thanks!

thheller12:12:29

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

🙏 3
mauricio.szabo15:12:44

@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.szabo15:12:28

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 🙂

victorb15:12:05

@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