Running node.js code from a Rails application

I am trying to integrate some node.js code with my Rails application. It's basically a js file with some code that will run in the background.

I followed these steps:

  • Added code in the root directory of the rails app in the test_node.js file.
  • Now I am passing the value to my system using the exec ruby ​​function like exec "node test_node.js".
  • This works great, but it interrupts my server from processing any requests.
  • To send it to the background, I tried using nohup, for example: exec "nohup node test_node.js".
  • The server crashes when you run this code.

I am a Rails developer and have never worked on a node application so I have no idea if I am doing this correctly or not.

+3


source to share


1 answer


exec

replaces the current running process with a new one. Thus, to run something in the background, you should fork

, and exec

in children.

fork { exec('node', 'test_node.js', ...) }

      



nohup

not required.

See also Ruby, Difference between exec, system and% x () or Backticks

+3


source







All Articles