Which is the right way to integrate Julia program with Node.js

I have some algorithms developed in Python. The algorithms monitoring the movement of about 1000 ships in the port and calculate the risk, which will be used for industrial purpose.

I am planning to rewrite the algorithm in Julia. My colleagues are developing the GUI using Node.js. I need to let the Node.js code call my algorithm in Julia.
Call frequency: 1 time / second
Data exchange: Julia code will return some matrixes, and the size is not very large.

Some potential solutions:
(1) Web API
which Julia package is ready to use? is HTTP.jl proper ?

(2) Is it easy to compile Julia code to DLL and let Node.jl call as using the DLLs created from c/c++ code?
I will write the code in the style as static programming language. Theoretically, it is possible to compile to DLL.

I am familiar with C++. I want to try new technology and possibly save some time :slightly_smiling_face: Parallel computing of Julia would also be useful for this case.

What is the best way to do this work ? Thanks!

2 Likes

I don’t know if this is the best way, but this is one way to do it: A-simple-TCP-example. Especially a named pipe (Windows) or UNIX domain socket is the best way to do inside one computer. An example how to do that in node.js: linter-julia server.js

2 Likes

Yeah, I think TCP + JSON is the most sensible way to do something like that (and is what all such projects I know of use).

1 Like

Here is a minimum working example:

julia> using Sockets

julia> using JSON

julia> pipe = ""

julia> if Sys.iswindows()
           pipe = "\\\\.\\pipe\\testsocket"
       else
           pipe = tempname()
       end
"/var/folders/3z/1kq4lzyd39l1c2gl5c0pzvy80000gp/T/julia0Q6Mrr"

julia> @async begin
           server = listen(pipe)
           while true
               sock = accept(server)
               @async while isopen(sock)
                   input = JSON.parse(sock)
                   JSON.print(sock, Dict("data" => rand(input["input"])))
              end
           end
       end
Task (runnable) @0x000000010b2a61d0

julia> clientside = connect(pipe)
Base.PipeEndpoint(RawFD(0x00000015) open, 0 bytes waiting)

julia> for i in 1:10
           JSON.print(clientside,Dict("input" => i))
           println(JSON.parse(clientside))
       end
Dict{String,Any}("data"=>Any[0.559774])
Dict{String,Any}("data"=>Any[0.89733, 0.513331])
Dict{String,Any}("data"=>Any[0.0675964, 0.381228, 0.618926])
Dict{String,Any}("data"=>Any[0.961195, 0.645998, 0.696614, 0.539933])
Dict{String,Any}("data"=>Any[0.927968, 0.328774, 0.969805, 0.959731, 0.169076])
Dict{String,Any}("data"=>Any[0.33247, 0.929461, 0.162861, 0.270243, 0.827589, 0.666108])
Dict{String,Any}("data"=>Any[0.0766846, 0.996563, 0.299863, 0.996271, 0.904049, 0.622786, 0.315614])
Dict{String,Any}("data"=>Any[0.245844, 0.991654, 0.590529, 0.515608, 0.557779, 0.331809, 0.0752659, 0.205465])
Dict{String,Any}("data"=>Any[0.841952, 0.514019, 0.676124, 0.668515, 0.72262, 0.231272, 0.425817, 0.512281, 0.949894])
Dict{String,Any}("data"=>Any[0.360244, 0.484267, 0.946671, 0.162157, 0.569451, 0.416769, 0.433126, 0.0784071, 0.432733, 0.521421])

julia> 

@zhangliye it would be nice if you can click the solution button.

6 Likes

Thanks for all the suggestions! Currently, I am the only one using Julia in my institute, I will try to show that Julia can do the same thing in an easy and better way.

2 Likes