My first guess: you try to publish while the publisher is not fully set up. Can you try waiting for a second (e.g. using usleep(1000*1000)) between creating your publisher and sending the first message?