Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My setup

  • macOS 10.15.7
  • zeromq.js: v6.0.0-beta.6
  • node.js: v14.4.0

I'm using ZeroMQ.js to communicate between my app's frontend and backend.

The pattern in use is zmq.Pair. And the client, a.k.a. the frontend, can receive messages from the server, a.k.a. the backend, just fine.

Code



class UiBridge {
    constructor(address="tcp://127.0.0.1:25555") {
        this.address = address;
        this.socket = new zmq.Pair();
    }

    async Start() {
        await this.socket.connect(this.address);

        console.log("frontend: Connected to backend.");

        const listen = async () => {
            for await (const [backendRequest] of this.socket) {
              switch (backendRequest.toString()) {
                case 'msg1':
                  onMsg1();

                  // THIS Send works!
                  // the receiver gets the reply.

                  this.Send("reply");
                  break;

                case 'term':
                  this.Stop();
                  break;

                default:
                  console.error(`undefined message from backend: ${backendRequest}`);
              }
            }
        }
      
        listen();
    }
    
    async Stop() {
        if (!this.socket.closed) {
          this.socket.close()
        }
    }

    async Send(cmd) {
        try {
            await this.socket.send(cmd);
            alert(`sent msg: ${msg}`);
        } catch (err) {
            alert(`send failed due to : ${err}`);
        }
    }
}

Note that if I send message inside the listening loop on receiving any message from the other side, then the send works.

Problem

But if I send messages outside of this listening loop anywhere else in the frontend code, then the other end never receives the sent message, and no exceptions are caught. Like in a button callback


function onToggleButton() {

    // This is never received!

    gUiBridge.Send("from frontend");
}

The button callback is triggered and I see the alert coming up. I've made sure that I can already receive messages from the server before clicking on buttons.

Workaround

I had to create another pair of PUSH/PULL to send messages from anywhere of my frontend to backend, so that I'm not blocked by this issue. But this is suboptimal.

Question

Is this by design or am I doing anything wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
357 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...