Friday, 1 May 2020

Is ".once" appropriate to use for simple message passing between processes?

I'm kind of new to using multiple processes in node.js. I want different routes of my app to send messages to worker process, then I want to get a response back. Just simple message passing, I really don't want to mess around with buffers if I can avoid it.const childProcess = require('child_process'); const express = require('express') /** @type */ const workers = {} for(const name of ['a', 'b']) { const worker = childProcess.fork('./app.js', [name]) workers[name] = worker } const app = express() app.get(`/v/:name`, (req, res) => { const {name} = req.params if (!(name in workers)) { res.sendStatus(404) } else { const worker = workers[name] worker.once('message', msg => res.send(msg)) worker.send('hello world') } }) app.listen(3000) Is it ok to define one off events like that, with the .once method?

Submitted May 02, 2020 at 05:01AM by LAC-Tech

No comments:

Post a Comment