graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

Is there not a single example of java + nodejs interop?

Open balayanv opened this issue 1 year ago • 4 comments

I understand that one can simply run unmodified node projects with this, that's nice but most people would look at graal so they can use node modules out there from java.

I remember doing all sorts of crazy hacks, setting up shared task queues because node has its event loop and you cant just call into it without being on that thread...

Is there not even a single example of java interacting with a simple node code? call node code from java and vice versa...

I remember also having to launch my code with custom shell scripts because the executable is node and intellij cant understand that its actually a fully powered jvm that runs node... so i had to transform, manually grab classpaths feed them into node command properly so i can just launch the app with graalnodejs (loosing any capability to debug stuff at the same time)...

This was a while ago, so maybe there is a better way to do things or i was doing some unnecessary magic to begin with? either way it was bad dev experience...

balayanv avatar Sep 03 '24 08:09 balayanv

If you are using intellij it is quite easy to launch image

intllij provides $ClassPath when you attach Build on Before Launch tab. I also I think, I can attach debugger on jvm to if i can get the path of java-agent of intellij but can't find it yet.

In this image I'm using kotlin the top level class is just `Main3. It can be any other classes. e.g) com.example.Runner ...

pbk20191 avatar Oct 21 '24 11:10 pbk20191

Oh i see i was going trough different route trying to fool intellij into treating it as a jvm...

balayanv avatar Oct 21 '24 11:10 balayanv

for me the best way to hop thread between node.js and java thread. I create node.js binding which create ThreadSafeFunction and pass as ExternalWrapper.

but somehow when I reference externalwrapper from java side. JVM reference retain the native reference of the main_loop's uv_async_t. So memory leak happens #817 #819

But I hope this will be solved in GraalJS 24.2.0 which provides EventLoopExecutor!

pbk20191 avatar Oct 21 '24 11:10 pbk20191

@pbk20191 thanks for the replies, you'd do me a huge favour if you could share some sample code doing java <-> node interop i remember diving into this mess years ago, now i was revisiting the project cos i have some new ideas but i dont remember much from my findings. I do remember how annoying it was to get async code work without node runtime though.

Either way i know you dont have to do it, you dont seem to be even from oracle, its just odd that they have no sample code for this at all...

balayanv avatar Oct 23 '24 13:10 balayanv

repo.zip

unpack the file and clone the git-bundle like below

unzip repo.zip
git clone repo.bundle

I only tested it using Intellij. You have to specify GraalNodejs path in running configuration. Also run npm install in the project path. Since you need to build native-nodejs-extension.

You have to check several settings carefully. I hope this could be helpful ( though, in my opinion I will wait for graal 24.2.0 for convenience)

there is two main entry Point

  1. Main -> supports only window

  2. Main2 -> use node_thread_safe_function so It should works in most platform

pbk20191 avatar Oct 26 '24 07:10 pbk20191

thanks, main 2 is what i wanted appreciate the help

balayanv avatar Oct 26 '24 09:10 balayanv

@balayanv I realized that I forgot to add some fix. I re uploaded the zip file.

pbk20191 avatar Oct 27 '24 02:10 pbk20191

Check this out this library, which simplifies calling NodeJS code from Java/Kotlin

https://mikehearn.github.io/nodejvm/

mikehearn avatar Feb 02 '25 17:02 mikehearn

Yeah I've used that in a different project and it still is clunky to use, its just puzzling to me that oracle doesn't put any emphasis on really good things about graalvm instead there is so much material about native image nonesense.

Ironically oracle doesn't make the best JVM, it doens't make the best GC (i'd argue they don't make the best jvm language either) but the one thing they do that no one else does is polyglot... and they do not bother to put some dev materials to actually make use of this...

I remember my first thought when i saw what graal can do was to do jupiter notebook like thing where you can mix python kotlin and javascript however you like instantly get access to all the libraries from biggest 3 dev communities out there...

Either way thanks for help, i've figured out way around my issues a while a go, i'll close the issue.

balayanv avatar Feb 02 '25 17:02 balayanv

Maybe, you could check this out.

caoccao avatar Feb 02 '25 19:02 caoccao