简体   繁体   中英

Node.js and zmq

I have a strange issue with a basic pubsub application with node and zmq:

a client is publishing strings to a broker, the problem is that the broker only receives the first line. At network level I've noticed that only the first message is sent then the next calls to .send() function have no effect (no packets are sent) so I suppose the problem is in the client/publisher. I used the example code provided in the official guide which works perfectly, the only difference in my code is that I use prototype to have a reusable structure. (I didn't paste subscriber's code because is not relevant and took some other not relevant stuff out)

relevant part of the client/publisher:

Publisher = function(zmq, pport) {
    this.logread = spawn('tail', ['-n0', '-f', '/var/log/auth.log']);
    this.publisher = zmq.socket('req');
    this.pport = pport;
};

Publisher.prototype.start = function() {
    var self = this;
    this.publisher.connect('tcp://127.0.0.1:' + this.pport);
    this.logread.stdout.on('data', function(data){
        self.publisher.send(data.toString());
        console.log(data.toString());
    });
};

relevant part of the broker:

Broker = function(zmq, bpport, bsport) {
    this.server = zmq.socket('rep');
    this.bpport = bpport;
    this.bsport = bsport;
};

Broker.prototype.start = function() {
    this.server.on('message', function(request) {
        console.log(request.toString());
    });

    this.server.bind('tcp://127.0.0.1:' + this.bsport, function(err) {
        if (err)
            console.log(err);
    });

};

You are talking about publish subscribe pattern, but in your code, you create a req socket, and in the broker a rep socket, which is for the request-reply pattern. The request-reply pattern is strictly need to send first, than receive, see the api docs docs, or read more from the guide

I suppose you should use pub socket on the client side, and a sub socket on the other side, but don't know what do you want to achieve, maybe a different pattern would fit your needs better.

so I'll answer my question: the server must send a reply to the client, until then the client will not send more messages

server.send('OK');

I also suppose there is a different way to achieve this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM