0

I have a node.js server running on a Raspberry Pi 3 B+. (I'm using node because I need the capabilities of a bluetooth library that works well).

Once the node server picks up a message from a bluetooth device, I want it to fire off an event/command/call to a different python script running on the same device.

What is the best way to do this? I've looked into spawning child processes and running the script in them, but that seems messy... Additionally, should I set up a socket between them and stream data through it? I imagine this is done often, what is the consensus solution?

5
  • 1
    Running a child process is how you would run a python script. That's how you do it from nodejs or any other program (besides a python program). Commented May 17, 2019 at 4:24
  • 2
    There are dozens of options for communicating between the python script and the nodejs program. The simplest would be stdin/stdout which are automatically set up for you when you create the child process, but you could also make the nodejs into a local http server that the python script could communicate with or vice versa. Or, set up a regular socket between the two. Commented May 17, 2019 at 4:28
  • Could you elaborate on why you think spawning child process seems "messy" ? Commented May 17, 2019 at 9:10
  • Yea! I was thinking about this as a client-server type relationship. The python script would already be running, so spawning a new one doesn't seem necessary. Commented May 17, 2019 at 19:21
  • Thanks @jfriend00 for the local server idea, I will go with this! I'll post the code I use in the answer when I'm finished. Commented May 17, 2019 at 19:22

1 Answer 1

1

Running a child process is how you would run a python script. That's how you do it from nodejs or any other program (besides a python program).

There are dozens of options for communicating between the python script and the nodejs program. The simplest would be stdin/stdout which are automatically set up for you when you create the child process, but you could also give the nodejs app a local http server that the python script could communicate with or vice versa.

Or, set up a regular socket between the two.

If, as you now indicate in a comment, your python script is already running, then you may want to use a local http server in the nodejs app and the python script can just send an http request to that local http server whenever it has some data it wants to pass to the nodejs app. Or, if you primarily want data to flow the opposite direction, you can put the http server in the python app and have the nodejs server send data to the python app.

If you want good bidirectional capabilities, then you could also set up a socket.io connection between the two and then you can easily send messages either way at any time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.