Forum Libraries, Components, and Plugins
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

MQTT publish with system.net.mqtt upon button_click event

L3xL3x USMember ✭✭

Hi, I been working on a small peice of code in Xamarin Forms (my first project) to send a mqtt message upon a button press.
Ive written the code to publish the mqtt message to the test.mosquitto.org server within a C# console app and that seems to be working OK.

`using System;
using System.Net.Mqtt;
using System.Text;

namespace MqttTest.Client
{
class Program
{
const string topic = "test/test/button";

    static void Main (string[] args)
    {
        var config = new MqttConfiguration { Port = 1883 };
        var client = MqttClient.CreateAsync("test.mosquitto.org", config).Result;
        var clientId = "myClientID";
        string message = "test";


        client.ConnectAsync (new MqttClientCredentials (clientId)).Wait ();
        client.SubscribeAsync (topic, MqttQualityOfService.AtLeastOnce).Wait ();
        //Publishes "message" Var 
        client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{message}")), MqttQualityOfService.AtLeastOnce).Wait();
    }
}

}
`

Ive now written a very simple xamarin cross platform app with 1 button, 1 button_clicked event and the code for the button clicked event which was written in the above console app.

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Net.Mqtt;

namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
const string topic = "test/test/button";

    private void Button_Clicked(object sender, EventArgs e)
    {
        var config = new MqttConfiguration { Port = 1883 };
        var client = MqttClient.CreateAsync("test.mosquitto.org", config).Result;
        var clientId = "clientIdhGHvpYY9uM";
        string message = "Hello";


        client.ConnectAsync(new MqttClientCredentials(clientId)).Wait();
        client.SubscribeAsync(topic, MqttQualityOfService.AtLeastOnce).Wait();
        client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{message}")), MqttQualityOfService.AtLeastOnce).Wait();
    }
}

}
`

Upon testing the app in the emulator, it loads up OK but when I click the button, it freezes the app and does not publish to the test mqtt server.

Can anyone offer any advice on how to get this working

thank you

Tagged:

Answers

  • L3xL3x USMember ✭✭

    Anyone......!

  • L3xL3x USMember ✭✭

    This is not a very good forum

  • batmacibatmaci DEMember ✭✭✭✭✭

    did you find a solution for your problem?

  • L3xL3x USMember ✭✭
    Yes, the xamarin plugin and support on here is not good. I ended up using another plugin. And it worked straight away
  • LucasSossaiLucasSossai Member ✭✭✭

    @L3x said:
    Yes, the xamarin plugin and support on here is not good. I ended up using another plugin. And it worked straight away

    Which plugin are you using now?
    Is it good?

  • Terry90Terry90 Member

    @L3x what plugin did you use?

  • woutswouts Member

    @L3x

    hi there

    I'm very new to making apps with Xamarin or just in general. I'm currently trying to make a basic app that can send and recieve MQTT messages. I have made a windows forms app that can send and recieve messages with M2MQTT. This was very easy btw, but with Xamarin apparently not so easy :( (Or maybe it's just too hard for me). I would appreciate it if you would send over the code/file of your app that you got working :)

    English isn't my native language so excuse the mistakes.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @L3x How ironic!!! You saying the forum is trash when you respond to all these people looking for the answer you found with "found another plugin and it works" w/out marking an answer and saying which plugin... ridiculous

  • NitishsantpurNitishsantpur Member ✭✭✭

    @L3x can u please tell what plugin did u use

Sign In or Register to comment.