-
Notifications
You must be signed in to change notification settings - Fork 522
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
What is the right way to use this code (Paho MQTT) as GoRoutine and pass messages via channel to publish via websockets #357
Comments
Hi @bcashier, When you say "passing messages in mass while doing high latency tasks" I assume that you mean that you want to send the messages asynchronously (so the message is handled by a different go-routine than your main code is running on). If that is the case then a very simple change to your initial example will give you that:
The only reason that your initial code stopped until the message was sent was that you called token.Wait(). If you don't care about errors (and you are not checking for them so I assume you dont care) then there is little point in calling token.Wait() (it just waits until the message is sent; the message will go out whether you call token.Wait or not).. If you want to log any errors you could use something like:
Note that there are a few more things that you need to do if message delivery is critical (but as you are not checking for errors I'm assuming its not). In terms of the code you found; I suspect that this would add complexity you dont need (and more info would be required to work this out; for example the MqttProtocol struct is not defined within the bit you pasted). PS. It's better to ask this kind of question somewhere like stack overflow (and I can see that you have now done this). |
Same question was asked on Stack Overflow and answer has been accepted. |
This question was solved by @MattBrittan on StackOverflow. Thanks again. |
As standard code I am using to publish message for testing purpose:
This works well ! but passing messages in mass while doing high latency tasks, performance of my program will be low, so I have to use goroutine and channel.
So, I was looking for a way to make a Worker inside goroutine for PUBLISHING messages to the browser using Paho MQTT library for GOlang, I had a hard time to find a better solution that feet my need, but after some searches, I found this code:
This code do exactly what I am looking for !
But as noob in Golang, I can't figure out how to run
START()
function inside my main function and what argument to pass !And espacially, how I will process to pass messages to the worker (Publisher) using channel ?!
Your help will be appreciated !
The text was updated successfully, but these errors were encountered: