Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is nodejs representing Reactor or Proactor design pattern?

Many articles online demonstrates nodejs as an example of reactor pattern. Isn't it rather proactor?

As far as I understand, the difference between the two is:

  1. reactor handles events in a single thread (synchronously),
  2. proactor handles events is multiple threads (asynchronously) with completion callbacks.

For example in this article:

Reactor Pattern is an idea of non-blocking I/O operations in Node.js. This pattern provides a handler(in case of Node.js, a callback function) that is associated with each I/O operation. When an I/O request is generated, it is submitted to a demultiplexer.

Isn't it actually definition of proactor?

like image 999
user2449761 Avatar asked Oct 15 '25 05:10

user2449761


1 Answers

The distinction has nothing to do with multithreading. It is as follows:

  • Reactor - I want to read from a socket, so I subscribe to a data-is-available kind of event and, and when it fires react to it by synchronously reading the available amount.
  • Proactor - I want to read from a socket, so I initiate a reading operation (one that proactively reads the data, without waiting for me to react to it's availability), and subscribe to some kind of read-is-complete notification, wherein the read data is immediately available to me.

Node has both kinds of APIs, e.g. stream.ReadableStream#readable/stream.ReadableStream#read are a Reactor interface, while fs.readFile is a Proactor.

like image 123
yuri kilochek Avatar answered Oct 17 '25 21:10

yuri kilochek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!