Last active
July 6, 2021 20:50
-
-
Save akashrajkn/e0d7e672e8b394544aab4338996a6343 to your computer and use it in GitHub Desktop.
IPC with pipes (Node.js): Write to pipe, Read processed data from pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const { spawn, fork } = require('child_process'); | |
const path_a = 'pipe_a'; | |
const path_b = 'pipe_b'; | |
let fifo_b = spawn('mkfifo', [path_b]); // Create Pipe B | |
fifo_b.on('exit', function(status) { | |
console.log('Created Pipe B'); | |
const fd = fs.openSync(path_b, 'r+'); | |
let fifoRs = fs.createReadStream(null, { fd }); | |
let fifoWs = fs.createWriteStream(path_a); | |
console.log('Ready to write') | |
setInterval(() => { | |
console.log('----- Send packet -----'); | |
fifoWs.write(`${new Date().toISOString()}`); | |
}, 1000); // Write data at 1 second interval | |
fifoRs.on('data', data => { | |
now_time = new Date(); | |
sent_time = new Date(data.toString()); | |
latency = (now_time - sent_time); | |
console.log('----- Received packet -----'); | |
console.log(' Date : ' + data.toString()); | |
console.log(' Latency: ' + latency.toString() + ' ms'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeaah, you need to wait until pipes open, thanks !