Search code examples
node.jssocketstcpzeromqpublish-subscribe

zeromq.js - Await connection


The documentation for connect method says,

Connects to the socket at the given remote address and returns immediately. The connection will be made asynchronously in the background.

But, await does not seem to be applicable as shown in their example of subscriber code.

subscriber.js

const zmq = require("zeromq")

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

  sock.connect("tcp://127.0.0.1:3000") //Happens async; can we await this?
  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()

Also, what error(s) maybe raised by the connect() method? I provided an 'obscene' port number such as, 8124000, to connect. I was hoping for some error messages to be raised.


Solution

  • Q : "what error(s) maybe raised by the connect() method?"


    The Error(s) part

    The ZeroMQ native API distinguishes ( unchanged since v2.1 ) these errors for this :

    EINVAL
    The endpoint supplied is invalid.
    EPROTONOSUPPORT
    The requested transport protocol is not supported.
    ENOCOMPATPROTO
    The requested transport protocol is not compatible with the socket type.
    ETERM
    The ØMQ context associated with the specified socket was terminated.
    ENOTSOCK
    The provided socket was invalid.
    EMTHREAD
    No I/O thread is available to accomplish the task.

    Yet your actual observer is dependent on the zeromq.js re-wrapping these principal states, so the best next step is to re-read the wrapper source code, so as to see, how these native API error states get actually handled inside the zeromq.js-wrapper.

    The remarks :

    The following socket events can be generated. This list may be different depending on the ZeroMQ version that is used.

    Note that the error event is avoided by design, since this has a special behaviour in Node.js causing an exception to be thrown if it is unhandled.

    Other error names are adjusted to be as close to possible as other networking related event names in Node.js and/or to the corresponding ZeroMQ.js method call. Events (including any errors) that correspond to a specific operation are namespaced with a colon :, e.g. bind:error or connect:retry.

    are nevertheless quite warning, aren't they?


    The await part

    The MCVE-code ( as-is ) is unable to reproduce the live-session, so best adapt the MCVE-code so as to get run-able and we can proceed further on this.