pythoncnanomsgpynng

nng to pynng communication is not working


I am trying to make a c-program server that publishes images via nng, and a python client via pynng that subscribes to the images.

For some reason i cannot connect these 2 parts and I dont know why. C/C++-program compiles and runs fine and so does python program, but something is published on the C/C++-program, nothing is recieved on the python client. Client and server runs on the same machine. Here is my C/C++-code for the server:

#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>

int main(int argc, char* argv[]){

    nng_socket sock;
    int rv;
    const char *url = "tcp://0.0.0.0:2234";

    if ((rv = nng_pub0_open(&sock)) != 0) {
            fatal("nng_pub0_open", rv);
    }
    if ((rv = nng_listen(sock, url, NULL, 0)) < 0) {
            fatal("nng_listen", rv);
    }

    // camera setup code omitted

    while(1){
        INT r = IS_SUCCESS;
        char* frame =(char*) d.processNextFrame(50,r); // grab image. if not successful, continue loop
        if (r != IS_SUCCESS || !frame)
            continue;

        std::cout << "frame caught... "<< bpp * nImgW * nImgH<< std::endl;

        if ((rv = nng_send(sock, frame, bpp * nImgW * nImgH, 0)) != 0) {
            fatal("nng_send", rv);
        }else{
            std::cout << "Frame Sent... "<< std::endl;
        }
    }
}

Here is my python code for the client:

from pynng import Sub0, Timeout
address = 'tcp://127.0.0.1:2234'
sub2 = Sub0(dial=address, recv_timeout=5000)
print(sub2.recv())

Can someone help me understand why this is not working?


Solution

  • You have to subscribe to topics on your subscribing socket, or you won't receive anything. In order to receive all messages, subscribe to the empty string:

    from pynng import Sub0, Timeout
    address = 'tcp://127.0.0.1:2234'
    # note the additional keyword argument, topics:
    # =============================================
    sub2 = Sub0(dial=address, recv_timeout=5000, topics=b'')
    print(sub2.recv())
    

    Here are the pynng docs for Sub0.

    I'm the creator of pynng. It would probably be nice to be clear in the docs that not subscribing to anything means you'll never receive anything; this is a common point of confusion.