You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This section describes how to receive cloud-to-device messages using the [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class in the Azure IoT SDK for .NET.
13
+
This section describes how to receive cloud-to-device messages.
14
14
15
15
There are two options that a device client application can use to receive messages:
16
16
17
-
***Polling**: The device application checks for new IoT Hub messages using a code loop (for example, a `while` or `for` loop). The loop executes continually, checking for messages.
18
17
***Callback**: The device application sets up an asynchronous message handler method that is called immediately when a message arrives.
18
+
***Polling**: The device application checks for new IoT Hub messages using a code loop (for example, a `while` or `for` loop). The loop executes continually, checking for messages.
19
19
20
-
### Declare a DeviceClient object
20
+
### Required device NuGet Package
21
21
22
-
[DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) includes methods and properties necessary to receive messages from IoT Hub.
22
+
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
23
23
24
-
For example:
24
+
Add these `using` statements to use the device library.
25
25
26
26
```csharp
27
-
staticDeviceClientdeviceClient;
27
+
usingMicrosoft.Azure.Devices.Client;
28
+
usingMicrosoft.Azure.Devices.Shared;
28
29
```
29
30
31
+
### Connect a device to IoT Hub
32
+
33
+
A device app can authenticate with IoT Hub using the following methods:
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods required to receive messages on the device.
43
+
30
44
### Supply the connection parameters
31
45
32
46
Supply the IoT Hub primary connection string and Device ID to `DeviceClient` using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring) method. In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
@@ -35,12 +49,42 @@ Supply the IoT Hub primary connection string and Device ID to `DeviceClient` usi
35
49
*`transportSettings` - Interface used to define various transport-specific settings for `DeviceClient` and `ModuleClient`. For more information, see [ITransportSettings Interface](/dotnet/api/microsoft.azure.devices.client.itransportsettings).
36
50
*`ClientOptions` - Options that allow configuration of the device or module client instance during initialization.
37
51
38
-
This example calls `CreateFromConnectionString`to define the `DeviceClient` connection IoT hub and device ID settings.
52
+
This example connects to a device using the `Mqtt` transport protocol.
To receive callback cloud-to-device messages in the device application, the application must connect to the IoT Hub and set up a callback listener to process incoming messages. Incoming messages to the device are received from the IoT Hub message queue.
68
+
69
+
Using callback, the device application sets up a message handler method using [SetReceiveMessageHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setreceivemessagehandlerasync). The message handler is called then a message is received. Creating a callback method to receive messages removes the need to continuously poll for received messages.
70
+
71
+
Callback is available only using these protocols:
72
+
73
+
*`Mqtt`
74
+
*`Mqtt_WebSocket_Only`
75
+
*`Mqtt_Tcp_Only`
76
+
*`Amqp`
77
+
*`Amqp_WebSocket_Only`
78
+
*`Amqp_Tcp_only`
79
+
80
+
The `Http1` protocol option does not support callbacks since the SDK methods would need to poll for received messages anyway, which defeats the callback principle.
81
+
82
+
In this example, `SetReceiveMessageHandlerAsync` sets up a callback handler method named `OnC2dMessageReceivedAsync`, which is called each time a message is received.
83
+
84
+
```csharp
85
+
// Subscribe to receive C2D messages through a callback (which isn't supported over HTTP).
Console.WriteLine($"\n{DateTime.Now}> Subscribed to receive C2D messages over callback.");
44
88
```
45
89
46
90
### Polling
@@ -109,31 +153,6 @@ while (!stopPolling)
109
153
}
110
154
```
111
155
112
-
### Callback
113
-
114
-
To receive callback cloud-to-device messages in the device application, the application must connect to the IoT Hub and set up a callback listener to process incoming messages. Incoming messages to the device are received from the IoT Hub message queue.
115
-
116
-
Using callback, the device application sets up a message handler method using [SetReceiveMessageHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setreceivemessagehandlerasync). The message handler is called then a message is received. Creating a callback method to receive messages removes the need to continuously poll for received messages.
117
-
118
-
Callback is available only using these protocols:
119
-
120
-
*`Mqtt`
121
-
*`Mqtt_WebSocket_Only`
122
-
*`Mqtt_Tcp_Only`
123
-
*`Amqp`
124
-
*`Amqp_WebSocket_Only`
125
-
*`Amqp_Tcp_only`
126
-
127
-
The `Http1` protocol option does not support callbacks since the SDK methods would need to poll for received messages anyway, which defeats the callback principle.
128
-
129
-
In this example, `SetReceiveMessageHandlerAsync` sets up a callback handler method named `OnC2dMessageReceivedAsync`, which is called each time a message is received.
130
-
131
-
```csharp
132
-
// Subscribe to receive C2D messages through a callback (which isn't supported over HTTP).
Console.WriteLine($"\n{DateTime.Now}> Subscribed to receive C2D messages over callback.");
135
-
```
136
-
137
156
### Receive message retry policy
138
157
139
158
The device client message retry policy can be defined using [DeviceClient.SetRetryPolicy](/dotnet/api/microsoft.azure.devices.client.deviceclient.setretrypolicy).
@@ -144,35 +163,46 @@ The message retry timeout is stored in the [DeviceClient.OperationTimeoutInMilli
144
163
145
164
The .NET/C# SDK includes a [Message Receive](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/getting%20started/MessageReceiveSample) sample that includes the receive message methods described in this section.
146
165
147
-
## Send cloud-to-device messages
166
+
## Create a backend application
148
167
149
168
This section describes essential code to send a message from a solution backend application to an IoT device using the [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class in the Azure IoT SDK for .NET. As discussed previously, a solution backend application connects to an IoT Hub and messages are sent to IoT Hub encoded with a destination device. IoT Hub stores incoming messages to its message queue, and messages are delivered from the IoT Hub message queue to the target device.
150
169
151
170
A solution backend application can also request and receive delivery feedback for a message sent to IoT Hub that is destined for device delivery via the message queue.
152
171
153
-
### Declare a ServiceClient object
172
+
### Add service NuGet Package
154
173
155
-
[ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) includes methods and properties necessary to send messages from an application through IoT Hub to a device.
174
+
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
156
175
157
-
```csharp
158
-
staticServiceClientserviceClient;
159
-
```
176
+
### Connect to IoT hub
177
+
178
+
You can connect a backend service to IoT Hub using the following methods:
Supply the IoT Hub primary connection string to `ServiceClient` using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring) method. In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
187
+
##### Supply the connection string
188
+
189
+
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring). In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
164
190
165
191
*`transportType` - `Amqp` or `Amqp_WebSocket_Only`.
166
192
*`transportSettings` - The AMQP and HTTP proxy settings for Service Client.
167
193
*`ServiceClientOptions` - Options that allow configuration of the service client instance during initialization. For more information, see [ServiceClientOptions](/dotnet/api/microsoft.azure.devices.serviceclientoptions).
168
194
169
-
This example creates the `ServiceClient` object using the IoT Hub connection string.
195
+
This example creates the `ServiceClient` object using the IoT Hub connection string and default `Amqp` transport.
Use [sendAsync](/dotnet/api/microsoft.azure.devices.serviceclient.sendasync) to send an asynchronous message from an application through the cloud (IoT Hub) to the device. The call is made using the AMQP protocol.
This section describes how to receive cloud-to-device messages using the [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class from the Azure IoT SDK for Java.
14
14
15
-
For a Java-based device application to receive cloud-to-device messages, it must connect to IoT Hub, then set up a callback listener and message handler to process incoming messages from IoT Hub. The device application should also be able to detect and handle disconnects in case the device-to-IoT Hub message connection is broken.
15
+
For a Java-based device application to receive cloud-to-device messages, it must connect to IoT Hub, then set up a callback listener and message handler to process incoming messages from IoT Hub.
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-deviceclient(java-lang-string-com-microsoft-azure-sdk-iot-device-iothubclientprotocol)) object instantiation requires these parameters:
30
39
@@ -39,11 +48,15 @@ The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#co
Use the [setMessageCallback](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?com-microsoft-azure-sdk-iot-device-deviceclient-setmessagecallback(com-microsoft-azure-sdk-iot-device-messagecallback-java-lang-object)) method to define a message handler method that is notified when a message is received from IoT Hub.
@@ -146,7 +159,7 @@ client.open(true);
146
159
147
160
**HandleMessages**: a sample device app included with the [Microsoft Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples), which connects to your IoT hub and receives cloud-to-device messages.
148
161
149
-
## Send cloud-to-device messages
162
+
## Create a backend application
150
163
151
164
This section describes how to send a cloud-to-device message using the [ServiceClient](/java/api/com.azure.core.annotation.serviceclient) class from the Azure IoT SDK for Java. A solution backend application connects to an IoT Hub and messages are sent to IoT Hub encoded with a destination device. IoT Hub stores incoming messages to its message queue, and messages are delivered from the IoT Hub message queue to the target device.
152
165
@@ -174,34 +187,48 @@ import java.io.IOException;
174
187
importjava.net.URISyntaxException;
175
188
```
176
189
177
-
### Define the connection protocol
190
+
### Connect to the IoT Hub
191
+
192
+
You can connect a backend service to IoT Hub using the following methods:
Use [IotHubServiceClientProtocol](/java/api/com.microsoft.azure.sdk.iot.service.iothubserviceclientprotocol) to define the application-layer protocol used by the service client to communicate with an IoT Hub.
180
204
181
205
`IotHubServiceClientProtocol` only accepts the `AMQPS` or `AMQPS_WS` enum.
### Open the connection between application and IoT Hub
220
+
#####Open the connection between application and IoT Hub
198
221
199
222
[open](/java/api/com.microsoft.azure.sdk.iot.service.serviceclient?#com-microsoft-azure-sdk-iot-service-serviceclient-open()) the AMQP sender connection. This method creates the connection between the application and IoT Hub.
### Open a feedback receiver for message delivery feedback
206
233
207
234
You can use a [FeedbackReceiver](/java/api/com.microsoft.azure.sdk.iot.service.feedbackreceiver) to get sent message delivery to IoT Hub feedback. A `FeedbackReceiver` is a specialized receiver whose `Receive` method returns a `FeedbackBatch` instead of a `Message`.
@@ -254,6 +281,7 @@ if (feedbackBatch != null) {
254
281
255
282
### SDK send message samples
256
283
257
-
*[Service client sample](/java/api/overview/azure/iot?example) - Send message example, #1.
284
+
There are two send message samples:
258
285
286
+
*[Service client sample](/java/api/overview/azure/iot?example) - Send message example, #1.
259
287
*[Service client sample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/service/samples/getting%20started/ServiceClientSample) - Send message example, #2.
0 commit comments