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

**URL:** <https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 3, 2019, 6:11am UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223 "2019-01-03T06:11:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [January 3, 2019, 6:11am UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223/1 "2019-01-03T06:11:07Z")

</div>

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](https://github.com/JuliaWeb/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 🙂 Parallel computing of Julia would also be useful for this case.

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

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [January 4, 2019, 6:18am UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223/2 "2019-01-04T06:18:39Z")

</div>

> [@zhangliye](#):
>
> What is the best way to do this work ?

I don’t know if this is the best way, but this is one way to do it: [A-simple-TCP-example](https://docs.julialang.org/en/v1/manual/networking-and-streams/#A-simple-TCP-example-1). 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](https://github.com/AtomLinter/linter-julia/blob/v0.7.3/lib/server.js)

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [January 4, 2019, 9:04am UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223/3 "2019-01-04T09:04:22Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [January 4, 2019, 7:51pm UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223/4 "2019-01-04T19:51:55Z")

</div>

Here is a minimum working example:

```julia
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.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [January 5, 2019, 1:18pm UTC](https://discourse.julialang.org/t/which-is-the-right-way-to-integrate-julia-program-with-node-js/19223/5 "2019-01-05T13:18:46Z")

</div>

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.
