Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Uint8Array over PUB/SUB (v4.6.0) #268

Closed
jean-airoldie opened this issue Jul 12, 2018 · 3 comments
Closed

Sending Uint8Array over PUB/SUB (v4.6.0) #268

jean-airoldie opened this issue Jul 12, 2018 · 3 comments

Comments

@jean-airoldie
Copy link

jean-airoldie commented Jul 12, 2018

Hi,
I am trying to figure out how to send an Uint8Array over a PUB/SUB pattern.
Currently, the array I publish differs from the one I receive. Could this be caused
by the fact that the array is not encoded into binary and instead text?

Here is a minimal working example of this issue :

const zmq = require('zeromq')
const { expect }  = require('chai')

const ZMQ_ADDR = 'tcp://127.0.0.1:5000'

let sndArr = new Uint8Array([21,31]);

let pub = zmq.socket('pub');
pub = zmq.socket('pub')
pub.bindSync(ZMQ_ADDR)

let sub = zmq.socket('sub');
sub.connect(ZMQ_ADDR);
sub.subscribe('topic');
sub.on('message', function(topic, message) {
  let recvArr = new Uint8Array(message)
  expect(recvArr.toString()).to.equal(sndArr.toString())
});

pub.send(['topic', sndArr])

Example output :

...
AssertionError: expected [ 50, 49, 44, 51, 49 ] to equal [ 21, 31 ]
...
@jean-airoldie jean-airoldie changed the title Sending Uint8Array over PUB/SUB Sending Uint8Array over PUB/SUB (v4.6.0) Jul 12, 2018
@jean-airoldie
Copy link
Author

jean-airoldie commented Jul 12, 2018

Futhermore, if I try converting the Uint8Array to a buffer, the message is never received
by the SUB socket.

Here is a minimal working :

const zmq = require('zeromq')
const { expect }  = require('chai')

const ZMQ_ADDR = 'tcp://127.0.0.1:5000'

let sndArr = new Uint8Array([21,31]);
let buf = Buffer.from(sndArr);

let pub = zmq.socket('pub');
pub = zmq.socket('pub')
pub.bindSync(ZMQ_ADDR)

let sub = zmq.socket('sub');
sub.connect(ZMQ_ADDR);
sub.subscribe('topic');
sub.on('message', function(topic, message) {
  let recvArr = new Uint8Array(message)
  expect(recvArr.toString()).to.equal(sndArr.toString())
});

pub.send(['topic', buf])

This results in the program hanging forever because the message is never received
by the SUB socket.

@jean-airoldie
Copy link
Author

I tried using zeromq-ng (I was inspired by this issue) which solved my problem.

Here is a minimal working example :

// subscriber.js
const { expect }  = require('chai')
const zmq = require("zeromq-ng")

let sndArr = new Uint8Array([21,31]);

async function run() {
  const sock = new zmq.Subscriber

  sock.connect("tcp://127.0.0.1:5000")
  sock.subscribe("topic")
  console.log("Subscriber connected to port 5000")

  while (!sock.closed) {
    const [topic, msg] = await sock.receive()
    let recvArr = new Uint8Array(msg)
    expect(recvArr.toString()).to.equal(sndArr.toString())
    console.log("Received valid Uint8Array")
  }
}

run()
// publisher.js
const zmq = require("zeromq-ng")

let sndArr = new Uint8Array([21,31]);
let buf = Buffer.from(sndArr);

async function run() {
  const sock = new zmq.Publisher

  await sock.bind("tcp://127.0.0.1:5000")
  console.log("Publisher bound to port 5000")

  while (!sock.closed) {
    console.log("Sending Uint8Array")
    await sock.send(["topic", buf])
    await new Promise(resolve => setTimeout(resolve, 500))
  }
}

run()

Of course, this does not mean that the issue was fixed, rather that I will use zeromq-ng until it is (or until zeromq-ng is integrated into zeromq.js).

@rolftimmermans
Copy link
Member

Here is an update: a 6.0 beta has been released that incorporates all changes from ZeroMQ-NG. The API is exactly the same as that of the last ZeroMQ-NG release.

If there are any specific problems remaining please open a new issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants