pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

Issues across languages. Not working with Rust, Python and Node #146

Closed drbh closed 5 years ago

drbh commented 5 years ago

Hey ZQM4'ers,

I am having issues integrating Go + ZQM with my existing ecosystem... My Go ZMQ subscriber does not receive any messages!

I currently have a Rust publisher that emits data (a timestamp) every half second.

Clients in Python, Go and Node need to subscribe and act on the data. Both my Python and Node clients have no issues subscribing to the messages.

The Working Clients

Python Client

import zmq

loc = 'tcp://127.0.0.1:5555'

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt_string(zmq.SUBSCRIBE, '')
socket.connect(loc)
print(loc, "is live:", (not socket.closed))

while True:
    message = socket.recv()
    print(message)

Node Client

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

sock.connect('tcp://localhost:5555');
sock.subscribe('');
console.log('Subscriber connected to port 5555');

sock.on('message', function(topic, message) {
    console.log(topic.toString('utf8'))
});

The Not Working Client

When I start the Go client - no messages are received.

My go client..

package main

import (
    zmq "github.com/pebbe/zmq4"

    "fmt"
    "time"
)

func main() {
    //  Prepare our subscriber
    subscriber, _ := zmq.NewSocket(zmq.SUB)
    defer subscriber.Close()

    time.Sleep(1000 * time.Millisecond)

    subscriber.Connect("tcp://*:5555")
    subscriber.SetSubscribe("")

    for {
        // Read envelope with address
        address, _ := subscriber.Recv(0)
        fmt.Println(address)
    }
}

Am I doing something incorrect? Should I not expect zmq4 to work with my Rust publishers? Am I subscribing to the wrong topic (with the empty string?) The go sub works when I use a go pub - but not outside of the lang...

Thanks!

drbh commented 5 years ago

forgot to add the rust Publisher

use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
    let context = zmq::Context::new();
    let publisher = context.socket(zmq::PUB).unwrap();
    publisher
        .bind("tcp://127.0.0.1:5555")
        .expect("failed binding publisher");

    loop {
        let start = SystemTime::now();
        let since_the_epoch = start
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards");
        let s = format!("{:?}", since_the_epoch);
        publisher
            .send(&s, 0)
            .expect("failed sending first envelope");

        let mut child = Command::new("sleep").arg(".5").spawn().unwrap();
        let _result = child.wait().unwrap();
    }
}
pebbe commented 5 years ago

You are ignoring all errors. Check the errors, perhaps they give a clue.

Are Go en Rust using the same version of ZeroMQ? Both version 3, or both version 4?

pebbe commented 5 years ago

The Rust example doesn't compile. I get:

error[E0433]: failed to resolve. Use of undeclared type or module `zmq`
drbh commented 5 years ago

I should show the errors - I'll do some digging!

In order to compile the rust code you'll need to include zmq in the cargo.toml file. I've uploaded a repo with the working code here: https://github.com/drbh/gorustzmq

You should be able to run the Rust publisher and all the clients using the provided code.

drbh commented 5 years ago

I added error outputs to the Go code - but they both retun nil...

I seem to be using zmq 4.3.1 in all cases....

drbh commented 5 years ago

update!!!

I've changed

subscriber.Connect("tcp://*:5555")

to

subscriber.Connect("tcp://localhost:5555")

and it worked! guess *: isnt allowed in zqm4.

The updated code is in: https://github.com/drbh/gorustzmq

Thanks for the responses @pebbe