Lab 1 - Getting Started With Event Hubs
Lab 1 - Getting Started With Event Hubs
Azure
Lab 1 - Getting Started with Event Hubs
Overview
In this lab, you will create an Azure Event Hub and use it to collect event data from a client
application.
• A web browser
• A Microsoft account
• A Microsoft Azure subscription
• A Windows, Linux, or Mac OS X computer
• The lab files for this course
Note: To set up the required environment for the lab, follow the instructions in the Setup
document for this course. Specifically, you must have signed up for an Azure subscription and
installed Node.JS on your computer.
Note: The Microsoft Azure portal is continually improved in response to customer feedback. The
steps in this exercise reflect the user interface of the Microsoft Azure portal at the time of writing,
but may not match the latest design of the portal exactly.
Create a Namespace
Before you can create event hubs, you must create an event hub namespace.
1. In the Azure portal, browse to the event hubs you created earlier
2. Un the Navigation Pane (on your left search for Entities and click Event Hubs)
3. Click + Event Hub
• Name: Enter a unique name (and make a note of it!)
• Partition Count: 2
• Click Create
4. In the Azure portal, wait for the notification that the event hub has been created
1. Open a Node.JS console and navigate to the eventclient folder in the folder where you
extracted the lab files.
2. Enter the following command, and press RETURN to accept all the default options. This
creates a package.json file for your application:
npm init
3. Enter the following command to install the Azure Event Hubs package:
npm install azure-event-hubs
4. Use a text editor to edit the eventclient.js file in the eventclient folder.
5. Modify the script to set the connStr variable to reflect your shared access policy
connection string, as shown here:
var evthub = require('azure-event-hubs');
var client =
evthub.EventHubClient.createFromConnectionString(connStr)
setInterval(function(){
dev = 'dev' + String(Math.floor((Math.random() * 10) + 1));
val = String(Math.random());
client.send({ body: {device: dev, reading: val}});
console.log(dev + ": " + val);
}, 500);
1. Open a second Node.JS console, and navigate to the eventreader folder in the folder
where you extracted the lab files.
2. Enter the following command, and press RETURN to accept all the default options. This
creates a package.json file for your application:
npm init
3. Enter the following command to install the Azure IoT Hub package:
npm install azure-event-hubs
4. Use a text editor to edit the eventreader.js file in the eventreader folder.
5. Modify the script to set the connStr constant to reflect the shared access policy
connection string for your Event Hub, as shown here:
main().catch((err) => {
console.log(err);
});
6. Save the script and close the file.
10. Execute the newly updated script using
node eventclient.js
11. You should see some values on your console
12. Leavit running
4. After both scripts have been running for a while, stop them by pressing CTRL+C in each of the
console windows. Then close the console windows.
Note: You will use the resources you created in this lab when performing the next lab, so do not
delete them. Ensure that all Node.js scripts are stopped to minimize ongoing resource usage
costs.