Skip to content

Commit 92c464c

Browse files
authored
More Examples & Use Fully Qualified Links (#45)
1 parent e25dc74 commit 92c464c

File tree

3 files changed

+121
-10
lines changed

3 files changed

+121
-10
lines changed

Documentation/Events.md

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# Events
22

3-
This client has built in events to allow users to hook into any part of the client.
3+
This HiveMQ client has a large number of built in events to allow users to hook into any part of the client.
44

5-
This can be used to modify behavior, monitor activity or extend functionality.
5+
These events can be used to modify behavior, monitor activity or extend functionality.
66

77
## Examples
88

9-
### Display Connect Options Prior to Connecting
9+
The following serves as a few examples on how to utilize the built in event system.
1010

11-
The following serves as an example how to utilize events. This one simply prints out the HiveMQClientOptions prior to the connect command being sent to the broker.
11+
### Display Options Prior to Connecting
12+
13+
This one simply prints out the HiveMQClientOptions prior to the connect command being sent to the broker.
1214

1315
```C#
16+
using HiveMQtt.Client.Events;
17+
1418
private static void BeforeConnectHandler(object? sender, BeforeConnectEventArgs eventArgs)
1519
{
16-
Assert.NotNull(sender);
1720
if (sender is not null)
1821
{
1922
var client = (HiveMQClient)sender;
2023
Console.WriteLine("Connecting to Broker with the Options: {}", eventArgs.Options)
21-
24+
2225
}
2326
}
2427

@@ -30,7 +33,92 @@ client.BeforeConnect += BeforeConnectHandler;
3033
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
3134
```
3235

33-
## List of Supported Events
36+
### Taking Action After a Subscribe
37+
38+
Suppose you wanted to take some global action after every subscribe call made by the client.
39+
40+
```C#
41+
using HiveMQtt.Client.Events;
42+
43+
private static void AfterSubscribeHandler(object? sender, AfterSubscribeEventArgs eventArgs)
44+
{
45+
if (sender is not null)
46+
{
47+
var client = (HiveMQClient)sender;
48+
49+
// The result of the subscribe call
50+
// eventArgs.SubcribeResult
51+
52+
}
53+
}
54+
55+
// Later...
56+
57+
var client = new HiveMQClient();
58+
59+
client.BeforeConnect += BeforeConnectHandler;
60+
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
61+
var subscribeResult = await client.SubscribeAsync("district/9/level", MQTT5.Types.QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
62+
```
63+
64+
### Monitoring outgoing Publish Packets
65+
66+
The following can be used to monitor when publish packets are transmitted from the client. A potential debug vector in application development.
67+
68+
```C#
69+
using HiveMQtt.Client.Events;
70+
71+
private static void OnPublishSentHandler(object? sender, OnPublishSentEventArgs eventArgs)
72+
{
73+
if (sender is not null)
74+
{
75+
var client = (HiveMQClient)sender;
76+
77+
// The transmitted MQTT Publish packet
78+
// eventArgs.PublishPacket
79+
80+
// and the MQTT5PublishMessage
81+
// eventArgs.PublishPacket.Message
82+
83+
}
84+
}
85+
86+
// Later...
87+
88+
var client = new HiveMQClient();
89+
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
90+
91+
client.OnPublishSent += OnPublishSentHandler;
92+
93+
var result = await client.PublishAsync("district/7/count", "82", MQTT5.Types.QualityOfService.AtLeastOnceDelivery).ConfigureAwait(false);
94+
```
95+
96+
### Monitoring Subscribe Response Packets (SUBACK)
97+
98+
The following can be used to monitor SubAck responses from the broker
99+
100+
```C#
101+
using HiveMQtt.Client.Events;
102+
103+
private static void OnSubAckReceivedHandler(object? sender, OnSubAckReceivedEventArgs eventArgs)
104+
{
105+
if (sender is not null)
106+
{
107+
var client = (HiveMQClient)sender;
108+
109+
// The received SubAck packet
110+
// eventArgs.SubAckPacket
111+
}
112+
}
113+
114+
// Later...
115+
116+
var client = new HiveMQClient();
117+
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
118+
var subResult = await client.SubscribeAsync("district/9/level", MQTT5.Types.QualityOfService.ExactlyOnceDelivery).ConfigureAwait(false);
119+
```
120+
121+
## List of Supported Events
34122

35123
### General
36124

@@ -71,3 +159,7 @@ These events happen based on MQTT packet activity.
71159
| OnSubAckSent | `OnSubAckSentEventArgs` | `SubAckPacket` |
72160
| OnUnsubscribeSent | `OnUnsubscribeSentEventArgs` | `UnsubscribePacket` |
73161
| OnUnsubAckSent | `OnUnsubAckSentEventArgs` | `UnsubAckPacket` |
162+
163+
# See Also
164+
165+
* [Examples](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Documentation/Examples.md)

Documentation/Examples.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
These examples serve to help you get started quickly and easily. If you want to see another example here, feel free to let us know by [filing an issue](https://github.com/hivemq/hivemq-mqtt-client-dotnet/issues/new/choose). We'd be happy to help out.
12

23
## How to Wait on an Event
34

@@ -29,7 +30,7 @@ await client.DisconnectAsync().ConfigureAwait(false);
2930
await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
3031
```
3132

32-
#### Subscribe to Multiple Topics At Once With Varying QoS Levels
33+
## Subscribe to Multiple Topics At Once With Varying QoS Levels
3334

3435
```c#
3536
using HiveMQtt.Client.Options;
@@ -46,3 +47,21 @@ var result = await client.SubscribeAsync(options);
4647
* `client.Subscriptions` is updated with complete list of subscriptions made up to this point
4748
* each `Subscription` object has a resulting `ReasonCode` that represents the Subscribe result in `result.Subscriptions[0].ReasonCode`
4849

50+
## Securely Connect to a Broker with Basic Authorization Credentials
51+
52+
```C#
53+
var options = new HiveMQClientOptions()
54+
{
55+
Host = "b8293h09193b.s1.eu.hivemq.cloud",
56+
Port = 8883,
57+
UseTLS = true,
58+
UserName = "my-username",
59+
Password = "my-password",
60+
};
61+
62+
var client = new HiveMQClient(options);
63+
var connectResult = await client.ConnectAsync().ConfigureAwait(false);
64+
```
65+
## See Also
66+
67+
* HiveMQClient [Events](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Documentation/Events.md)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ We'd appreciate any feedback you have. Happy MQTT adventures!
1515
* **Opensource**: No blackbox code. Only trusted, tested and reviewed opensource code.
1616
* **Easy to Use**: Smart defaults, excellent interfaces and intelligent automation makes implementing a breeze.
1717
* **MQTT v5.0 compatible**: Backported versions 3.1.1 & 3.0 coming soon!
18-
* **Extensive Event System**: Hook into all parts of the client down to the packet level with [built in events](./Documentation/Events.md).
18+
* **Extensive Event System**: Hook into all parts of the client down to the packet level with [built in events](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Documentation/Events.md).
1919
* **Globally Compatible**: Built to be a fully compliant client compatible with all reputable MQTT brokers.
2020
* **Actively Maintained**: Built by the MQTT professionals that built HiveMQ (and do this for a living).
2121
* **Extensively Documented**: What good is it without excellent documentation?
@@ -98,7 +98,7 @@ await client.PublishAsync(
9898
).ConfigureAwait(false);
9999
```
100100

101-
For more examples that you can easily copy/paste, see our [Examples](./Examples/).
101+
For more examples that you can easily copy/paste, see our [Examples](https://github.com/hivemq/hivemq-mqtt-client-dotnet/blob/main/Documentation/Examples.md).
102102

103103
## Other MQTT Clients
104104

0 commit comments

Comments
 (0)