Asked by:
How to create mqtt client to publish/subscribe on web application(ASP.NET)?

Question
-
User-1586767941 posted
hello,
I am using HiveMQ broker to publish and subscribe. so I created wpf application to get the message it working fine.
and same thing I tried with ASP.net But it not working. while debugging I am able to get the data but not displaying it on form.Is there any other way to do this on web application or am I missing some thing? Plz give some idea.
note:
and i m using
this.Dispatcher.Invoke(() =>
what is the alternative for same behaviour in asp.net.
private MqttClient mqttClient; public MainWindow() { InitializeComponent(); mqttClient = new MqttClient(IPAddress.Parse(txtConnectionString.Text).ToString()); mqttClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;//event mqttClient.MqttMsgSubscribed += client_MqttMsgSubscribed;//event } private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { this.Dispatcher.Invoke(() => { lstSubscribe.Items.Add("Recevied..." + System.Text.Encoding.UTF8.GetString(e.Message)); }); } private void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e) { this.Dispatcher.Invoke(() => { lstSubscribe.Items.Add("Topic " + txtTopic.Text); lstSubscribe.Items.Add("Subscribed for id " + e.MessageId); lstSubscribe.Items.Add(" "); }); } private void btnConnect_Click(object sender, RoutedEventArgs e) { try { mqttClient.Connect(Guid.NewGuid().ToString()); lstPublish.Items.Add("*** Connected"); } catch (Exception ex) { lblError.Content = ex.Message.ToString(); } } private void btnSubscribe_Click(object sender, RoutedEventArgs e) { mqttClient.Subscribe(new string[] { txtTopic.Text }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); }
Tuesday, December 27, 2016 3:54 AM
All replies
-
User-1838255255 posted
Hi SushilBhat,
According to your description, as far as I know, this is a real time communication application, in the website, I think SignalR is a real time technology be used in web application. Please refer to these links about how to implement the application with SignalR:
Pushing Data: Integrating with ASP.NET SignalR Hubs:
http://henriquat.re/server-integration/signalr/integrateWithSignalRHubs.html
Tutorial: Server Broadcast with SignalR 2:
https://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
Learn About ASP.NET SignalR: (SignalR tutorial)
Best Regards,
Eric Du
Wednesday, December 28, 2016 9:36 AM -
User-1586767941 posted
Thank you Eric for your response, I'll try this...
Friday, December 30, 2016 4:39 AM -
User273014794 posted
Hi man,
Can you please share the full of code and what libraries did you use in visual studio? I have similar problem. I tried to use your code but I got some erros.
Friday, April 26, 2019 9:54 AM