Skip to main content

Connecting to an MQTT Broker

with Defaults

Without any options given, the HiveMQClient will search on localhost port 1883 for an unsecured broker.

If you don't have a broker at this location, see the next sections.

using HiveMQtt.Client;

// Connect
var client = new HiveMQClient();
var connectResult = await client.ConnectAsync().ConfigureAwait(false);

With Options

The HiveMQClientOptions class provides a set of options that can be used to configure various aspects of the HiveMQClient.

The easiest way to construct this class is to use HiveMQClientOptionsBuilder.

var options = new HiveMQClientOptionsBuilder().
WithBroker('candy.x39.eu.hivemq.cloud').
WithPort(8883).
WithUseTLS(true).
Build();

var client = new HiveMQClient(options);
var connectResult = await client.ConnectAsync().ConfigureAwait(false);

HiveMQClientOptionsBuilder Reference

To illustrate each and every possible call with HiveMQClientOptionsBuilder, see the following example:

using HiveMQtt.MQTT5.Types; // For QualityOfService enum

var options = new HiveMQClientOptionsBuilder()
.WithBroker("broker.hivemq.com")
.WithPort(1883)
.WithClientId("myClientId")
.WithAllowInvalidBrokerCertificates(true)
.WithUseTls(true)
.WithCleanStart(true)
.WithKeepAlive(60)
.WithAuthenticationMethod("UsernamePassword")
.WithAuthenticationData(Encoding.UTF8.GetBytes("authenticationData"))
.WithUserProperty("property1", "value1")
.WithUserProperties(new Dictionary<string, string> { { "property1", "value1" }, { "property2", "value2" } })
.WithLastWill(new LastWillAndTestament {
Topic = "lwt/topic",
PayloadAsString = "LWT message",
QoS = QualityOfService.AtLeastOnceDelivery,
Retain = true })
.WithMaximumPacketSize(1024)
.WithReceiveMaximum(100)
.WithSessionExpiryInterval(3600)
.WithUserName("myUserName")
.WithPassword("myPassword")
.WithPreferIPv6(true)
.WithTopicAliasMaximum(10)
.WithRequestProblemInformation(true)
.WithRequestResponseInformation(true)
.Build();

See Also