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

Any way to await connect() or listen for connection done / error events ? #585

Open
franklevasseur opened this issue Oct 2, 2023 · 0 comments

Comments

@franklevasseur
Copy link

franklevasseur commented Oct 2, 2023

Is your feature request related to a problem? Please describe.

I'm trying to publish an event to a server. Here's the code (this code works):

import * as zmq from "zeromq";
import { Config } from "./config";

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const runPub = async (config: Config) => {
  const publisher = new zmq.Publisher();

  publisher.connect(`tcp://127.0.0.1:${config.port}`);
  await sleep(100); // If I remove this, my code doesn't work... Any way to do this better?

  await publisher.send([
    "mychannel",
    `J${JSON.stringify({
      channel: "mychannel",
      formats: { "http-stream": { content: "a chunk of data\n" } },
    })}`,
  ]);
};

If I remove the await sleep(100) my code does not work (I do not receive the data on the subscriber side). I'd like a cleaner way of doing things.

I also tried listening on connect event but it doesnt work (I do not receive data on the subscriber side) and the program takes forever to exit.

import * as zmq from "zeromq";
import { Config } from "./config";

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const runPub = async (config: Config) => {
  const publisher = new zmq.Publisher();

  // does not work
  await new Promise<void>((resolve) => {
    publisher.events.on("connect", () => {
      console.log("connected");
      resolve();
    });
    publisher.connect(`tcp://127.0.0.1:${config.port}`);
  });

  await publisher.send([
    "mychannel",
    `J${JSON.stringify({
      channel: "mychannel",
      formats: { "http-stream": { content: "a chunk of data\n" } },
    })}`,
  ]);
};

Describe the solution you'd like

Connect should return a promise if it involves async operations.

Describe alternatives you've considered

If you can listen for some sort of connection_success and connection_error events, then its allright.

Additional context

  • I'm using zeromq@6.0.0-beta.17
  • The subscriber side is Pushpin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant