Created
February 21, 2022 23:38
-
-
Save mohas/5cbb5999ed281a9437eeefdf8b0139dc to your computer and use it in GitHub Desktop.
zeromq node.js example with router and multiple req sockets, CURVE authentication and identity
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
//most of the code on the internet is for version 5 so here is a workign sample | |
//hope it helps somebody | |
// Demonstrate request-reply identities | |
var zmq = require('zeromq') | |
var helper = require('./zhelpers') | |
const serverkey = zmq.curveKeyPair() | |
const clientkey = zmq.curveKeyPair() | |
const addr = "tcp://127.0.0.1:12345"//CURVE don't work for inproc schema | |
var sink = new zmq.Router({ | |
curveServer: true, | |
curveSecretKey: serverkey.secretKey, | |
curvePublicKey: serverkey.publicKey | |
}); | |
// First allow 0MQ to set the identity | |
var anonymous = new zmq.Request({ | |
curveSecretKey: clientkey.secretKey, | |
curvePublicKey: clientkey.publicKey, | |
curveServerKey: serverkey.publicKey | |
}); | |
anonymous.connect(addr); | |
// Then set the identity ourselves | |
var identified = new zmq.Request({ | |
routingId: 'peepee',//identity does not work when connecting to a router, instead use routingId | |
curveSecretKey: clientkey.secretKey, | |
curvePublicKey: clientkey.publicKey, | |
curveServerKey: serverkey.publicKey | |
}); | |
identified.connect(addr); | |
async function watch(){ | |
await sink.bind(addr); | |
for await (const msg of sink){ | |
helper.dumpFrames(msg) | |
} | |
} | |
async function send() { | |
await anonymous.send("ROUTER uses generated 5 byte identity"); | |
await identified.send("ROUTER uses REQ's socket identity"); | |
} | |
watch() | |
send() | |
setTimeout(function() { | |
anonymous.close(); | |
identified.close(); | |
sink.close(); | |
}, 250); |
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
// Return the buffer's length as a three-character, | |
// zero-padded string (e.g. printf's `%03d`) | |
function bufferLength(buffer) { | |
var lenStr = "" + buffer.length; | |
while (lenStr.length < 3) { | |
lenStr = "0" + lenStr; | |
} | |
return lenStr; | |
} | |
// Return the buffer's contents as printable text if every | |
// character is printable, or as hexadecimal otherwise | |
function formatBuffer(buffer) { | |
for (var i = 0; i < buffer.length; i++) { | |
if (buffer[i] < 32 || buffer[i] > 127) { | |
return buffer.toString("hex") | |
} | |
} | |
return buffer.toString("utf8"); | |
} | |
module.exports = { | |
dumpFrames: function() { | |
var frames; | |
if (arguments.length == 1) { | |
var arg = arguments[0]; | |
if (Array.isArray(arg)) { | |
// Single argument is an array of frames (buffers) | |
frames = arg; | |
} else { | |
// Single argument is a single frame (buffer) | |
frames = [arg]; | |
} | |
} else { | |
// Multiple arguments; each is a frame (buffer) | |
frames = Array.prototype.slice.call(arguments); | |
} | |
console.log("----------------------------------------"); | |
frames.forEach(function(frame) { | |
console.log("[%s] %s", bufferLength(frame), formatBuffer(frame)); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment