Reputation: 183
I am currently developing a client subscriber application using the M2MQTT that subscribes to an MQTT broker (mosquitto). I simply want the application to subscribe to the topic, and create a listener that will capture the message sent (sorry I am new to this, and I am a bit rusty on the vernacular).
protected void MqttProcessing()
{
// create client instance
MqttClient client = new MqttClient(MQTT_BROKER_HOST_NAME);
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "hello/world" }, new byte
{MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
The above is the recommended set up process to create a client instance, register to the message received, connect the client, then subscribe to the broker....ok fine I get that. I call this function in:
const string MQTT_BROKER_HOST_NAME = "brokernamehere.com";
protected void Page_Load(object sender, EventArgs e)
{
MqttProcessing();// call to the MQTT client setup steps.
}
Now I believe this is all working as it should,the MQTT connection is able to get a message when I send a message from my mosquitto MQTT client subscriber to the broker, the thing I am having problems wrapping my head around is, how can I grab the message once it gets to the client subscriber and have it persist, so that a user in a web browser can push a button and print the message out and see it in an asp:label tag? Here is what I have in the code behind:
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
try
{
Session["message"] = "Received = " +
Encoding.UTF8.GetString(e.Message) + " on topic " + e.Topic;
}
catch(HttpException ex)
{
Session["message"] = "ERROR:" + ex.Message;
}
}
If I do the above, the function barfs on the Session["message"] assignment. I debugged this and tried assigning the message to a string variable (tried a local and a global; contained in the partial class) and it is able to assign the message to the string variable...but obviously, it does not persist. The function that handles the button signal is simple:
protected void GetMQTT_Click(object sender, EventArgs e)
{
MessContentLabel.Visible = true;
MessContentLabel.Text = Convert.ToString(Session["message"]);
}
I was hoping to make use of the Session variable persistence properties, but I think I need something other than a Session variable. There are no simple examples for this on the web I have heard about others doing something similar successfully (see: MQTT client in MVC application ), but this gives little clues on my problem.
ADDITIONAL INFO:
So this is testing mosquitto ( https://mosquitto.org/ ) as an mqtt client publisher and broker and the M2MQTT C# .NET mqtt client library for a client subscriber.
I launch the mosquitto application as
$>mosquitto
Then I launch my web application via Visual Studio. which does the subscription and set up as described above
Next I launch the publisher via mosquitto:
$>mosquitto_pub -d -t hello/world -m "HelloWorld"
Again, when I debug via Visual Studio 2015 the listener in the web application receives it.... but I am unable to assign it to a variable that persists outside of(what I call) the listener method(?), shown above as MqttMsgPublishReceived. Again this is a simple test taking place entirely on my laptop.
Hope this additional information provides a clearer picture of what I am attempting.
Upvotes: 1
Views: 8046
Reputation: 3709
Please specify QOS 2 and test.
mosquitto_pub -d -t hello/world -m "HelloWorld" -q 2
Here are the details from online documentation.
QoS0, At most once: The message is delivered at most once, or it may not be delivered at all. Its delivery across the network is not acknowledged. The message is not stored. The message could be lost if the client is disconnected, or if the server fails. QoS0 is the fastest mode of transfer. It is sometimes called "fire and forget".
The MQTT protocol does not require servers to forward publications at QoS0 to a client. If the client is disconnected at the time the server receives the publication, the publication might be discarded, depending on the server implementation.
QoS1, At least once: The message is always delivered at least once. It might be delivered multiple times if there is a failure before an acknowledgment is received by the sender. The message must be stored locally at the sender, until the sender receives confirmation that the message has been published by the receiver. The message is stored in case the message must be sent again.
QoS2, Exactly once: The message is always delivered exactly once. The message must be stored locally at the sender, until the sender receives confirmation that the message has been published by the receiver. The message is stored in case the message must be sent again. QoS2 is the safest, but slowest mode of transfer. A more sophisticated handshaking and acknowledgement sequence is used than for QoS1 to ensure no duplication of messages occurs.
Cheers !
Upvotes: 1