How to subscribe hivemq broker for web application(asp.net)?

548 views
Skip to first unread message

Sushil Bhat

unread,
Dec 29, 2016, 1:00:39 AM12/29/16
to MQTT
I am using HiveMQ broker to publish and subscribe.

I want to develop a subscriber Web application using ASP.Net C# and
M2Mqtt.Net.dll ( version : 4.3.0.0 ). 


I am completely new for mqtt and hivemq, I dont know how to subscribe for web but i am able to subscribe for winforn/WPF application, same way i tried with web, but I think there is different way to do this I spent more time on google ans i got some link
using websocket we have to do this.
So my question is how to use websocket and javascript?
for this do i need some some library(like poho.js, mqtt.js) if yes how to use this? 
Can any one help me step by step what and how to do this?
thanks 
tink


Paul Fremantle

unread,
Dec 29, 2016, 4:03:24 AM12/29/16
to mq...@googlegroups.com
To use a MQTT/WS and JS library, you need to do the following.

1) You need a server that supports MQTT/WS.


(I used the browserify option and it worked pretty easily)

This creates a browserMqtt.js which you import. 

Then you can use code like this in your JS <script> section:

<script src="./browserMqtt.js"></script>
<script>

   var client = mqtt.connect("wss://ignite-iot.net:8443", options); 
      client.subscribe("/d/#");
 
      client.on("message", function(topic, payload) {
        message = JSON.parse(payload);
        if ('shortpress' in message) {
        document.getElementById('short').value=Date() ; 
        } else if ('longpress' in message) {
        document.getElementById('long').value=Date() ; 
        }  
      });
 
</script>  

Note my example is using secure WebSockets on port 8443, so yours will probably be different.

Paul


--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+unsubscribe@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at https://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.



--
Paul Fremantle
Doctoral Researcher, University of Portsmouth, School of Computing
Visiting Scientist, Institute of the Architecture of Application Systems, Stuttgart
Visiting Lecturer, Software Engineering Programme, Oxford University
Co-Founder, WSO2
Apache Member and Committer
twitter: pzfreo / skype: paulfremantle / blog: http://pzf.fremantle.org
Reply all
Reply to author
Forward
0 new messages