Нет
«сообщение» означает - вы получили новое входящее сообщение, полученное МПК (межпроцессное взаимодействие). NodeJS имеет только один встроенный способ отправки сообщений между процессами - process.send и child_process.send
Да
Конечно, вы можете использовать сторонние модули (например, node-ipc ) или сделать узел для интерпретации содержимого сообщения любым удобным вам способом:
main.js
const childProcess = require('child_process');
const fork = childProcess.fork(__dirname + '/fork.js');
const initIPC = require('./init-ipc');
initIPC(process);
fork.on('hello', event => {
console.log(event.hello);
});
// Say hello to forked process
fork.send({event: 'hello', hello: 'Hello from main process!'});
fork.js
const initIPC = require('./init-ipc');
initIPC(process);
process.on('hello', event => {
console.log(event.hello);
});
// Say hello to main process
process.send({event: 'hello', hello: 'Hello from forked process!'});
init-ipc.js
exports.initIPC = function (process) {
process.on('message', message => {
if (('event' in message) && typeof message.event === 'string') {
process.emit(message.event, message);
}
});
};