< Previous Challenge - Home - Next Challenge >
In this challenge, you’re going to add a Dapr input binding in the TrafficControlService. It’ll receive entry- and exit-cam messages over the MQTT protocol.
In this challenge you’ll focus on Dapr input bindings. The following diagram depicts how input bindings work:
For this hands-on challenge, you will add an input binding leveraging the Dapr binding building block. In the previous challenge, you implemented a Dapr output binding.
TrafficControlService
(TrafficController
class) to use the Dapr MQTT input binding to receive entry-cam and exit-cam messages over the MQTT protocol.Simulation
app to put trafficcontrol/entrycam
& trafficcontrol/exitcam
messages on the MQTT queue.
MqttTrafficControlService
to do this (look at the HttpTrafficControlService
as an example).Program
class to use this new service.This challenge targets the operation labeled as number 5 in the end-state setup:
Local
Azure
TrafficControlService
receives messages via its Dapr component.In order to connect to Mosquitto, you need to pass in a custom configuration file when starting it. With Docker, you can pass a configuration file when starting a container using a Volume mount. The folder Resources/Infrastructure/mosquitto
already contains a config file you can use.
Open a terminal window in VS Code and make sure the current folder is Resources/Infrastructure/mosquitto
.
Start a Mosquitto MQTT container by entering the following command: When running on Windows:
docker run -d -p 1883:1883 -p 9001:9001 -v $pwd/:/mosquitto/config/ --name dtc-mosquitto eclipse-mosquitto
When running on Mac or Linux:
docker run -d -p 1883:1883 -p 9001:9001 -v $(pwd)/:/mosquitto/config/ --name dtc-mosquitto eclipse-mosquitto
This will pull the docker image eclipse-mosquitto
from Docker Hub and start it. The name of the container will be dtc-mosquitto
. The server will be listening for connections on port 1883
for MQTT traffic.
The -v
flag specifies a Docker volume mount. It mounts the current folder (containing the config file) as the /mosquitto/config/
folder in the container. Mosquitto reads its config file from that folder.
To peak into the Mosquitto server, open a new terminal window and execute the following command:
docker logs dtc-mosquitto
var configuration = new MqttConfiguration()
{
KeepAliveSecs = 60,
Port = 1883
};
Use Azure IoT Hub & EventHub for deployments to Azure.
Create a IoT Device in Azure IoT Hub to represent your Simulation app.
az iot hub device-identity create --device-id simulation --hub-name <iot-hub-name>
Get the IoT Hub Connection String for the device you just created.
az iot hub device-identity connection-string show --device-id simulation --hub-name <iot-hub-name>
Use the Microsoft.Azure.Devices.Client
NuGet package to connect to the IoT Hub instead of the local MQTT broker.