/** * Used to send a message to the Python process. * @param {Object} interProcessObject The standard inter-process object. * @return {None} */ const sendToPython = (interProcessObject, verbose) => { if (verbose) { console.log(`<- out ${JSON.stringify(interProcessObject)}`); } if (socket) { socket.send(JSON.stringify(interProcessObject)); } }
ws.on("message", msg => { // When a new message is received by our WebSocket, // we broadcast it to all the connected clients console.log(`Message: ${msg}`); broadcast(msg); // we also publish it through our PUB socket using chat as aprefix followed by a space, // so the message will be published to all the // subscriptions using chat as a filter pubSocket.send(`chat ${msg}`); });
fs.watch(filename, () => { publisher.send(JSON.stringify({ type: 'changed', file: filename, timestamp: Date.now() })); });
resp.on('message', data => { const msg = JSON.parse(data); console.log(`${process.pid} received request for ${msg.filename}`); fs.readFile(msg.filename, (err, content) => { if (err) { throw err; } console.log(`${process.pid} sending response`); resp.send(JSON.stringify({ content: content.toString(), timestamp: Date.now(), pid: process.pid })); }); });
responder.on('message', data => { const msg = JSON.parse(data); console.log(`Receive request to get : ${msg.filename}`) fs.readFile(msg.filename, (err, content) => { if (err) { throw err; } console.log('Sending response...'); responder.send(JSON.stringify({ content: content.toString(), timestamp: Date.now(), pid: process.pid })); }); });
variationsStream(alphabet, maxLength) .on("data", combination => { console.log(combination); batch.push(combination); // Group the generated variations in batches of 10,000 items if (batch.length === BATCH_SIZE) { const msg = { searchHash: searchHash, variations: batch }; // Send task object to the next available worker ventilator.send(JSON.stringify(msg)); batch = []; } }) .on("end", () => { //send remaining combinations const msg = { searchHash: searchHash, variations: batch }; ventilator.send(JSON.stringify(msg)); });
// PUSH socket fromVentilator.on("message", buffer => { // For each message received, it iterates over the batch of words it contains, // then for each word it calculates the SHA1 checksum and tries to match it // against searchHash passed with the message. // When a match is found, the result is forwarded to the sink const msg = JSON.parse(buffer); const variations = msg.variations; variations.forEach(word => { console.log(`Processing: ${word}`); const shasum = crypto.createHash("sha1"); shasum.update(word); const digest = shasum.digest("hex"); if (digest === msg.searchHash) { console.log(`Found! => ${word}`); toSink.send(`Found! ${digest} => ${word}`); } }); });
router.on('message', (...frames) => dealer.send(frames));
dealer.on('message', (...frames) => router.send(frames));