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

MQTT: how do I resubscribe after reconnect? #570

Closed
neewbee opened this issue Jun 22, 2018 · 7 comments
Closed

MQTT: how do I resubscribe after reconnect? #570

neewbee opened this issue Jun 22, 2018 · 7 comments
Labels

Comments

@neewbee
Copy link

neewbee commented Jun 22, 2018

No description provided.

@neewbee
Copy link
Author

neewbee commented Jun 22, 2018

// +build example
//
// Do not build by default.

package main

import (
	"fmt"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/platforms/mqtt"
)

func main() {
	mqttAdaptor := mqtt.NewAdaptor("tcp://localhost:1883", "pinger")

	work := func() {
		mqttAdaptor.On("hello", func(msg mqtt.Message) {
			fmt.Println("hello")
		})
		mqttAdaptor.On("hola", func(msg mqtt.Message) {
			fmt.Println("hola")
		})
		data := []byte("o")
		gobot.Every(1*time.Second, func() {
			mqttAdaptor.Publish("hello", data)
		})
		gobot.Every(5*time.Second, func() {
			mqttAdaptor.Publish("hola", data)
		})
	}

	robot := gobot.NewRobot("mqttBot",
		[]gobot.Connection{mqttAdaptor},
		work,
	)

	robot.Start()
}

several seconds after stopping mqtt server, reconnect will happen. But Work no longer works

@deadprogram deadprogram changed the title how do I resubscribe after reconnect? MQTT: how do I resubscribe after reconnect? Jun 22, 2018
@deadprogram
Copy link
Member

@trevrosen this is one of the things we want to be able to solve with your latest work in progress.

@trevrosen
Copy link
Collaborator

@deadprogram indeed! Speaking of which you want me to proceed to PR w/ the implementation basically as it stands? Should we talk through some implications for existing installs etc?

@neewbee
Copy link
Author

neewbee commented Aug 15, 2018

Any update on this?
How can I help?

@grtfou
Copy link

grtfou commented Dec 19, 2018

I use two settings and it looks fine to my case. (gobot v1.12.0)
Maybe it's a solution if we're hard to monitor connection status.

mqttAdaptor.SetAutoReconnect(true)
mqttAdaptor.SetCleanSession(false)

and workaround for resubscribe topic (the same client id)

gobot.Every(30*time.Second, func() {
        mqttAdaptor.On("hello", func(msg mqtt.Message) {
			fmt.Println("hello")
	})
})

@trevrosen
Copy link
Collaborator

@neewbee sorry for my silence on this -- it fell through the cracks. There's a new construct in the dev branch called RobotWork which gives you some external control of the workloads a robot is using. Please have a look at that and let me know (via this thread) whether it gives you the semantics you need to help solve your issue.

@trevrosen
Copy link
Collaborator

Hi @neewbee I believe that this PR may also help you. I'm going to close this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants