1
votes

What I am trying to do is to have a C++ ZeroMQ server with publisher role and connect NodeJS subscriber clients.

I was able to get nodeJS server + nodejs client examples working, but I am not able to connect it using C++.

C++ code:

#include "zmq.hpp"

//  Convert string to 0MQ string and send to socket
auto s_send(zmq::socket_t & socket, const std::string & string) {    
    zmq::message_t message(string.size());
    memcpy(message.data(), string.data(), string.size());

    return socket.send(message, zmq::send_flags::none); 
}

int main() {
    std::cout << "Hello World!\n"; 

    zmq::context_t ctx(1);
    zmq::socket_t sock(ctx, ZMQ_PUB);
    sock.bind("tcp://*:5555");

    Sleep(1000);

    while (true) { 
        s_send(sock, "Hello from the publisher.");
        Sleep(1000);
    }
}

NodeJS code:

var zmq = require('zeromq');
var sock = zmq.socket('sub');

sock.connect("tcp://127.0.0.1:5555");
console.log("Subscriber connected to port 5555");

sock.on('message', function(msg){
    console.log('work: %s', msg.toString());
});

Any ideas what I am doing wrong? Is there any tutorial on interconnecting ZeroMQ from different languages? I was only able to find examples that use the same technologies on both side of the communication.

1

1 Answers

0
votes

Q : Any ideas what I am doing wrong?

ZeroMQ SUB-archetype receives nothing, unless it gets explicitly subscribed to some "Topic" ( ""-being a legal one for "everything" ) or it gets set a mirrored-logic of such Topic-filtering configuration on both PUB-side and SUB-side, given which a ZMQ_INVERT_MATCHING-flag self-explains how such subscriptions would start to work.

...was able to get nodeJS server + nodejs client examples working ... no chance ... no no
kein moeglichkeit - nein nein ...

Tip: your code does neither one of the must-have subscription settings. SUB-archetype default state is subscribed to nothing, literally, so the code was not able to get (it) working.


Q : but I am not able to connect it using C++

Well, before one claims a problem with C++ / API, better do test and document all such potential error-state(s):

MCVE-formulated problem must be re-test-able, otherwise the problem does not exist in Stack Overflow.

ZeroMQ has well crafted error-reporting tools, so in each step, one may read the error-state and report it as per receiving detailed reason for arriving into any kind of defined error-states: { EINVAL | ETERM | ENOTSOCK | EINTR | ... } as defined in ZeroMQ sources ( and well documented in the ZeroMQ API documentation, version by version ).