Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe() after Listen() doesn't works #105

Open
jkralik opened this issue May 6, 2021 · 0 comments
Open

Subscribe() after Listen() doesn't works #105

jkralik opened this issue May 6, 2021 · 0 comments

Comments

@jkralik
Copy link

jkralik commented May 6, 2021

When I create sub, and then Listen() and set OptionSubscribe, it never subscribe to accepted connections.

This is example of code which describe the issue:

        ctx := context.Background()
	sub := zmq4.NewSub(ctx)
	err := sub.Listen("tcp://*:1234") // listen
	if err != nil {
		log.Fatalf("could not listen: %v", err)
	}
	err = sub.SetOption(zmq4.OptionSubscribe, "") // subscribe for all messages
	if err != nil {
		log.Fatalf("could not subscribe to topic %v", err)
	}
	go func() {
		for {
			msg, err := sub.Recv() // never gets message because OptionSubscribe was not applied after connection was accepted.
			if err != nil {
				log.Printf("could not recv message: %v", err)
			} else {
				fmt.Printf("recv %+v\n", msg)
			}
		}
	}()

	pub := zmq4.NewPub(ctx)
	err = pub.Dial("tcp://*:1234")
	if err != nil {
		log.Fatalf("could not dial: %v", err)
	}
	for {
		msg := zmq4.NewMsgString("test abc")
		err := pub.Send(msg)
		if err != nil {
			log.Printf("could send message: %v", err)
		}
		fmt.Printf("msg send\n")
		time.Sleep(time.Second)
	}

Workaround: when I add one goroutine which in cycle call Subscribe() it works - but it is not effective and unreliable ...

        ctx := context.Background()
	sub := zmq4.NewSub(ctx)
	err := sub.Listen("tcp://*:1234")
	if err != nil {
		log.Fatalf("could not listen: %v", err)
	}
	err = sub.SetOption(zmq4.OptionSubscribe, "")
	if err != nil {
		log.Fatalf("could not subscribe to topic %v", err)
	}
	go func() {
		for {
			msg, err := sub.Recv()
			if err != nil {
				log.Printf("could not recv message: %v", err)
			} else {
				fmt.Printf("recv %+v\n", msg)
			}
		}
	}()

        // set subscription too all accepted socket
	go func() {
		for {
			err = sub.SetOption(zmq4.OptionSubscribe, "")
			if err != nil {
				log.Printf("could not subscribe to topic %v", err)
			}
			fmt.Printf("subscribe\n")
			time.Sleep(time.Second)
		}
	}()

	pub := zmq4.NewPub(ctx)
	err = pub.Dial("tcp://*:1234")
	if err != nil {
		log.Fatalf("could not dial: %v", err)
	}
	for {
		msg := zmq4.NewMsgString("test abc")
		err := pub.Send(msg)
		if err != nil {
			log.Printf("could send message: %v", err)
		}
		fmt.Printf("msg send\n")
		time.Sleep(time.Second)
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant