Endjin - Home

Send Data into Azure Event Hubs using Web Api’s HttpClient

by Howard van Rooijen

header-send-data-into-event-hubs-using-web-api

We’ve recently been working on an Internet of Things (IoT) Proof of Concept which involved sending device telemetry into Azure to be shaped, enriched, materialized into different data formats and then computationally processed, before being ingested into PowerBI.

As another part of the Proof of Concept was to design and manufacture the actual device, we knew we’d need to decouple these work streams and build a device emulator. As part of the data we wanted to collect was based on GPS and accelerometer data, we decided to create a simple Windows Phone application to use the inbuilt sensors to collect this type of data and then mock out the other data series we were interested in collecting.

Unfortunately there currently isn’t a Azure Service Bus client library that will run on Windows Phone and as the physical device we were prototyping also needed to be able to communicate to Azure Event Hub we needed a way to send the data via standard HTTP protocol. Luckily Azure Event Hubs (which is just Service Bus under the covers) exposes a ReST API and the platform layers abstractions like Brokered Messages on top of HTTP (and AMQP) to provide a simpler API.

The minimum requirement for sending a message to Azure Event Hubs involves sending a serialized payload to the correct queue path, which is of the format

https://NAMESPACE.servicebus.windows.net/EVENTHUB-NAME/publishers/PUBLISHER-NAME/messages

and that the HTTP message must have an Authorization header which contains a SharedAccessSignature token based the Shared Access Policies you have configured for your event hub, in this example we have two – one for the device sending data into Event Hubs (the producer) and another for the processor (the consumer):

2015-02-16_09-49-49

The next step is to make use of Sandrino Di Mattia’s very helpful Event Hubs Signature Generator tool, which will generate your signature based on the following information:

  • ServiceBus Namespace
  • Event Hub name
  • Publisher name
  • Sender Key Name (“device” in our screenshot above)
  • Sender Key (the primary key from the shared access key generator section for the policy for “device” – see screenshot below)
  • Mode (HTTP or AMQP) – choose HTTP
  • TTL – because this key will sit on the device, you either need to set a very long TTL or implement a mechanism to update this SAS token

2015-02-16_10-01-06

Once you have this Shared Access Signature, you can set the request header, serialize the payload and use the Web Api HttpClient to make the request:

If you want to test that messages are successfully being written into Event Hubs, Dan Rosanova has a very nice two part series (1, 2) on Event Processor Host best practices. Event Processor Host is a simple framework for processing high volumes of Event Hub data.

@HowardvRooijen

Sign up to Azure Weekly to receive Azure related news and articles direct to your inbox or follow on Twitter: @azureweekly

About the author

Howard spent 10 years as a technology consultant helping some of the UK's best known organisations work smarter, before founding endjin in 2010. He's a Microsoft Accelerator Mentor, and a Microsoft Azure MVP. You can follow him on Twitter via @HowardvRooijen