zeromq.js
zeromq.js copied to clipboard
PUB SUB CURVE
well im trying for weeks to enable a simple curve on pub and sub! i found example for v6 in type script but when i try to subscribe specific topic doesn't work!
could someone please give me an example in the code below using curve
`const zmq = require("zeromq")
async function run() { const sock = new zmq.Subscriber
sock.connect("tcp://127.0.0.1:3000") sock.subscribe("kitty cats") console.log("Subscriber connected to port 3000")
for await (const [topic, msg] of sock) { console.log("received a message related to:", topic, "containing message:", msg) } }
run()`
`const zmq = require("zeromq")
async function run() { const sock = new zmq.Publisher
await sock.bind("tcp://127.0.0.1:3000") console.log("Publisher bound to port 3000")
while (true) { console.log("sending a multipart message envelope") await sock.send(["kitty cats", "meow!"]) await new Promise(resolve => setTimeout(resolve, 500)) } }
run()`
found a solution is working if someone has a better solution share it thank you
` const zmq = require("zeromq")
const sock = new zmq.Subscriber
const serverPublickey = "am%ch(6JxceNGl+)s0G3Q=-kqC.9@s-b3H!4ldkr"
const Keypair = zmq.curveKeyPair()
// Setup encryption.
for (const s of [sock]) { s.curveServerKey = serverPublickey; s.curvePublicKey = Keypair.publicKey; s.curveSecretKey = Keypair.secretKey; } async function run() { sock.connect("tcp://127.0.0.1:3000") sock.subscribe("kitty cats") console.log("Subscriber connected to port 3000")
for await (const [topic, msg] of sock) { console.log("received a message related to:", topic, "containing message:", msg) } }
run()
// pub
const zmq = require("zeromq") const sock = new zmq.Publisher
const publicKey = "am%ch(6JxceNGl+)s0G3Q=-kqC.9@s-b3H!4ldkr"
const Keypair = zmq.curveKeyPair()
// Setup encryption.
for (const s of [sock]) { s.curveServer = true; s.curvePublicKey = publicKey; s.curveSecretKey = Keypair.secretKey; }
async function run() {
await sock.bind("tcp://127.0.0.1:3000") console.log("Publisher bound to port 3000")
while (true) { console.log("sending a multipart message envelope") await sock.send(["kitty cats", "meow!"]) await new Promise(resolve => setTimeout(resolve, 500)) } }
run()`