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

Modify topic before publishing #192

Open
xorduna opened this issue Mar 30, 2023 · 0 comments
Open

Modify topic before publishing #192

xorduna opened this issue Mar 30, 2023 · 0 comments

Comments

@xorduna
Copy link

xorduna commented Mar 30, 2023

Hi,

We have some legacy devices that send to a topic that should be changed.

I tried to implement the modification of the message with a plugin that uses the server.OnMsgArrived hook, but it seems that I can modify the payload but not the topic.

here is the code:

func (t *Topicrw) OnMsgArrivedWrapper(pre server.OnMsgArrived) server.OnMsgArrived {
	return func(ctx context.Context, client server.Client, req *server.MsgArrivedRequest) error {
		fmt.Println("Msg arrived")
		req.Publish.TopicName = []byte("/another/topic")
		req.Message.Payload = []byte("my other payload")
		req.Message.Topic = "/changed/topic"
		return nil
	}
}

Following the code described in this issue #24. I tried to republish the message with this code:

func (t *Topicrw) OnMsgArrivedWrapper(pre server.OnMsgArrived) server.OnMsgArrived {
	return func(ctx context.Context, client server.Client, req *server.MsgArrivedRequest) error {
		fmt.Println("Msg arrived")
		req.Publish.TopicName = []byte("/another/topic")
		req.Message.Payload = []byte("my other payload")
		req.Message.Topic = "/changed/topic"
		m := req.Message
		m.Topic = "/my/other/topic"
		m.Payload = []byte("lets change the payload")
		t.s.Publisher().Publish(m)
		req.Drop()
		return nil
	}
}

With the code above, a new message is published to another topic and the incoming message is supressed.

Is this code the right way to go?

Thank you very much!!!

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