2

I have two nodejs(serve) in the same server running at same time, I want to call a function from one to another, they are running with forever, it is like running normally like nodejs file.js

What can I do?

Things that I think I can do...

1.- create a server side in each with udp, and client side in each? and call the function when it requires

2.- join two server in one file and run it... but i don't want it

My question is what can I do?, is there another option for this?, I want different options from mine...?

DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
  • Are those two scripts or two *servers*? Because, if they are real servers they already have a TCP/UDP that you can call. If they are just two scripts doing some stuff and you need to share functionality between them, make a node module with that shared functionality and include the module in both scripts. – Tomalak Nov 12 '17 at 17:08

2 Answers2

1

Regardless of them being on a single machine or not, I'd recommend Redis pub-sub for cross-process communication without any strict requirements.

You might also want to fork those off of one main process to establish some communication, but that's rather unusual if you want a practical solution. https://nodejs.org/api/child_process.html Also described here as the default solution. Communicating between two different processes in Node.js

Redis pub-sub should be more future proof and harder to accidentally break for you.

https://gist.github.com/reu/5342276

naugtur
  • 16,827
  • 5
  • 70
  • 113
1

Use process.fork/send

If both processes are node, node core provides a simple, event-driven IPC mechanism. You will however have to make the one process the "master" meaning that it starts the other.

http://nodejs.org/docs/latest/api/all.html#all_child_process_fork_modulepath_args_options

  • event-based
  • bi-directional
  • efficient
  • uses only OS resources
  • in-memory

taken from: Inter-process event emitter for Node.js?

ThatBrianDude
  • 2,952
  • 3
  • 16
  • 42
  • this option isn't for me, undertand that I have to add the second server in the first and when run the first the second server is running like child – DarckBlezzer Nov 12 '17 at 20:24