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

Publish with retain doesn't retain #37

Closed
bldrdash opened this issue Mar 13, 2022 · 3 comments · Fixed by #42
Closed

Publish with retain doesn't retain #37

bldrdash opened this issue Mar 13, 2022 · 3 comments · Fixed by #42
Labels
bug Something isn't working

Comments

@bldrdash
Copy link

Publish works fine except for honoring the retain flag. Broker works fine with external publishing with retain.
Using master branch: 460f0ef

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	mqtt "github.com/mochi-co/mqtt/server"
	"github.com/mochi-co/mqtt/server/listeners"
	"github.com/mochi-co/mqtt/server/listeners/auth"
)

func main() {
	sigs := make(chan os.Signal, 1)
	done := make(chan bool, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sigs
		done <- true
	}()

	server := mqtt.New()
	tcp := listeners.NewTCP("t1", ":1883")
	server.AddListener(tcp, &listeners.Config{Auth: new(auth.Allow)})

	// server.Serve doesn't block, so no need for goroutine, but for some
	// reason, we have to delay a LONG time for Serve's goroutines to start-up
	// > 1 second on a 5900X !!
	server.Serve()
	fmt.Println("\nServer is running")
	fmt.Println("Sleeping for 2 seconds")
	time.Sleep(2 * time.Second)

	// Publish with retain - doesn't work
	server.Publish("direct/publish", []byte("Published from broker"), true)
	fmt.Println("\nMessage published to \"direct/publish\" with retain")

	fmt.Println("\nTry running: mosquitto_sub -v -t \"direct/publish\"")

	<-done
}
go run main.go

# In another terminal window

# The following shows nothing
mosquitto_sub -v -t "direct/publish"

# But this works:
mosquitto_pub -t "mosquitto/publish" -m "from mosquitto_pub" -r
mosquitto_sub -v -t "mosquitto/publish"

Am I missing something?

@bldrdash bldrdash changed the title Publish with retain doesn't work Publish with retain doesn't retain Mar 14, 2022
@mochi-co mochi-co added the bug Something isn't working label Mar 15, 2022
@wind-c
Copy link
Contributor

wind-c commented Mar 16, 2022

Error logic is as follows:
trie.go: RetainMessage method always return 0 if len(msg.payload) > 0
server.go:retainMessage method updates topic's retain message when q=1,and deletes stored retain message if q is not equal to 1.

@bldrdash
Copy link
Author

#42 PR confirmed working for me

@mochi-co
Copy link
Collaborator

Fixed in v1.1.2 branch as per @bldrdash :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants