Resources

Using JavaScript to Access Azure Event Hubs

July 18, 2024 by OpenObserve Team
Azure Event Hub Receiver

Introduction to Accessing Azure Event Hubs with JavaScript

Want to add real-time event streaming to your JavaScript applications? Buckle up! Azure Event Hubs and JavaScript can become your dynamic duo for handling high-volume data flows. By leveraging Event Hubs' scalability and message delivery guarantees, you can build robust and responsive web experiences.

Introduction to Accessing Azure Event Hubs with JavaScript

Image Credit

This guide will equip you with the knowledge to send and receive events using JavaScript libraries, allowing you to seamlessly integrate event-driven functionality into your applications.

Integration Between JavaScript and Azure Event Hubs

JavaScript and Azure Event Hubs can be integrated to enable real-time communication between web applications and event streams. Here's a breakdown of the key aspects:

  • Client-side Libraries: Azure Event Hubs provides JavaScript libraries that allow you to send and receive events from your web applications running in the browser (Node.js is used for server-side communication). These libraries handle the communication protocol and message framing, simplifying the interaction with Event Hubs.
  • Event Hubs Service: Azure Event Hubs is a highly scalable pub/sub service. It acts as a central message broker, ingesting a high volume of events from various sources and delivering them to registered consumers (your JavaScript application in this case).
  • Event Hubs Entities: Within Event Hubs, you'll define namespaces, event hubs, and consumer groups. Namespaces organize your Event Hubs resources. Event hubs are where the actual event streams reside. Consumer groups allow for multiple consumers (JavaScript applications) to receive events concurrently from the same event hub, with each group having its own independent view of the message stream.
  • Message Model: Events are essentially JSON objects containing data you want to transmit. The JavaScript libraries handle serialization and deserialization of these messages for seamless exchange.

Communication Flow

Sending Events:

  • Your JavaScript application creates an event object containing the data you want to send.
  • The library connects to the specified Event Hub using the connection string (containing authentication details).
  • The library transmits the event object to the chosen event hub.

Receiving Events:

  • Your JavaScript application creates a receiver object linked to the desired consumer group and event hub.
  • The receiver listens for incoming events from the stream.
  • When an event arrives, the library deserializes it into a usable JavaScript object for your application to process.

Get Started for FREE with OpenObserve

Benefits of Leveraging Azure Event Hubs in JavaScript Applications

Here's why integrating Azure Event Hubs with your JavaScript applications can be beneficial:

  • Scalability and High Throughput: Event Hubs can handle millions of events per second, making it ideal for applications dealing with large volumes of data.
  • Decoupling and Asynchronous Communication: Event Hubs decouples event producers (your JavaScript app) from consumers. This allows for independent development and scaling of different parts of your system. Messages are delivered asynchronously, improving responsiveness of your web application.
  • Reliable Delivery: Event Hubs offer message persistence and delivery guarantees, ensuring events are not lost even in case of transient failures.
  • Flexibility and Agnostic Nature: Event Hubs are agnostic to the message content and format (JSON in this case). This allows you to send various data types from your JavaScript application.
  • Real-time Processing: Events are delivered as soon as they are published, enabling real-time processing and reactions within your application.
  • Centralized Management: Azure Event Hubs provides a centralized platform for managing event streams and monitoring their health.

Overall, integrating Azure Event Hubs with JavaScript applications unlocks powerful capabilities for building scalable, reliable, and event-driven web experiences.

In the next section, you will learn about the prerequisites required for JavaScript event hubs integration.

Prerequisites for JavaScript Azure Event Hubs Integration

Here are the prerequisites for integrating JavaScript with Azure Event Hubs! Here's a breakdown of each point.

Understanding Azure Event Hubs

Event Hubs act as a central message broker, allowing real-time communication between various applications. They can ingest millions of events per second and ensure reliable delivery.

Requirements

  • Azure Subscription: You'll need an Azure subscription to create and access Event Hubs resources. You can sign up for a free trial if you don't have one already.
  • Node.js LTS Installation: JavaScript runs in the browser, but for server-side communication with Event Hubs, you'll need Node.js (preferably a Long-Term Support version for stability) installed on your development machine.
  • Visual Studio Code (Optional): While not strictly mandatory, using an IDE like Visual Studio Code can provide a more streamlined development experience with features like code completion and debugging.

Setting Up an Event Hubs Namespace and Event Hub

This is where you'll create the infrastructure for your event streams. You'll need to use the Azure portal to create:

  • Azure Event Hubs namespace. This acts as a container for your event hubs.
  • Within the namespace, create an event hub: This is where the actual stream of events will reside.

Once you have these prerequisites in place, you'll be ready to dive into the code and start integrating Azure Event Hubs with your JavaScript applications!

In the next section, you will explore the codes that help in setting up the development environment.

Get Started for FREE with OpenObserve

Setting Up the Development Environment

Here are the details explanations with code for setting up the development environment:

Installing npm packages: @azure/event-hubs, @azure/identity

To install the required npm packages, run the following command:

bash
npm install @azure/event-hubs @azure/identity

This command installs the @azure/event-hubs package for working with Azure Event Hubs and the @azure/identity package for authentication.

Azure provides two authentication options for Event Hubs: passwordless and connection string.

Passwordless Authentication

Passwordless authentication is recommended as it provides a more secure way of authenticating with Azure Event Hubs. To use passwordless authentication, you need to create an Azure Active Directory (AAD) application and grant it the necessary permissions to access the Event Hub.

Here is an example of how to use passwordless authentication:

javascript
const { EventHubClient } = require('@azure/event-hubs');
const { DefaultAzureCredential } = require('@azure/identity');

const credential = new DefaultAzureCredential();
const eventHubClient = new EventHubClient(
  'https://<event_hub_name>.servicebus.windows.net/<event_hub_name>',
  credential
);

// Use the eventHubClient to send and receive events.

Connection String Authentication

Connection string authentication is an older method of authentication that uses a connection string to authenticate with Azure Event Hubs. This method is less secure than passwordless authentication and should be avoided if possible.

Here is an example of how to use connection string authentication:

javascript
const { EventHubClient } = require('@azure/event-hubs');

const connectionStr = 'Endpoint=sb://<event_hub_name>.servicebus.windows.net/;SharedAccessKeyName=<shared_access_key_name>;SharedAccessKey=<shared_access_key_value>;SharedAccessPolicyName=<shared_access_policy_name>';
const eventHubClient = new EventHubClient(connectionStr);

// Use the eventHubClient to send and receive events

In this section, we have explored the two authentication options available for Azure Event Hubs: passwordless and connection string. We have also provided examples of how to use each authentication method.

In the next section, you will see how to authenticate the application to Azure.

Authenticating the Application to Azure

Comparing passwordless authentication to connection string methods

Passwordless authentication and connection string methods are two different approaches to authenticating with Azure. Here is a comparison of the two:

Feature Passwordless Authentication Connection String Methods
Security More secure Less secure
Convenience More convenient Less convenient
Examples Windows Hello, Microsoft Authenticator app, FIDO2 security keys, and SMS or Email codes Azure Active Directory (AAD) connection strings

Assigning Roles via Azure portal, Azure CLI, or PowerShell

Roles can be assigned via the Azure portal, Azure CLI, or PowerShell. Here are the steps for each method:

Azure Portal

  • Log in to the Azure portal.
  • Navigate to the Azure Active Directory (AAD) section.
  • Select the "Roles" tab.
  • Click on the "New role" button.
  • Choose the role you want to assign.
  • Select the users or groups you want to assign the role to.
  • Click on the "Assign" button.

Azure CLI

  • Install the Azure CLI.
  • Run the following command to assign a role:
    bash
    az role assignment create --role <role_name> --assignee <user_or_group> --scope /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.EventHub/namespaces/<namespace_name>/eventhubs/<event_hub_name>

PowerShell

Install the Azure PowerShell module.

Run the following command to assign a role:

powershell
$role = Get-AzRoleDefinition -Name <role_name>
$assignee = Get-AzADUser -UserPrincipalName <user_or_group>
New-AzRoleAssignment -RoleDefinition $role -Assignee $assignee -Scope /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.EventHub/namespaces/<namespace_name>/eventhubs/<event_hub_name>

Configuring the Environment for Secure Access

To configure the environment for secure access, follow these steps:

  • Enable Azure Active Directory (AAD) Integration: Enable AAD integration in your Azure environment to use Azure Active Directory for authentication.
  • Configure Azure Active Directory (AAD) Settings: Configure AAD settings such as the tenant ID, client ID, and client secret.
  • Enable Multi Factor Authentication (MFA): Enable MFA to add an extra layer of security to your Azure environment.
  • Configure Conditional Access: Configure conditional access to control access to Azure resources based on specific conditions.
  • Enable Azure Active Directory (AAD) Domain Services: Enable AAD Domain Services to manage domain services and simplify authentication in hybrid environments.

By following these steps, you can configure your Azure environment for secure access and ensure that your applications and services are properly authenticated and authorized.

In the next couple of sections you will learn how to send and receive events to Azure Event Hubs.

Sending Events to Azure Event Hubs

Creating a JavaScript Application to Send Events

To create a JavaScript application to send events to Azure Event Hubs, you can use the Azure Event Hubs JavaScript client library. Here are the steps to create a basic application:

  • Install the Azure Event Hubs JavaScript client library:
    bash
    npm install @azure/event-hubs
  • Create a new JavaScript file:
    javascript
    const { EventHubClient } = require('@azure/event-hubs');

// Create an instance of the EventHubClient

const eventHubClient = new EventHubClient(
  'https://<event_hub_name>.servicebus.windows.net/<event_hub_name>',
  '<shared_access_key_name>',
  '<shared_access_key_value>'
);

// Send an event to the event hub

eventHubClient.send({
  body: 'Hello, World!',
  partitionKey: 'my_partition_key'
});

Step-By-Step Guide to Implement Event Sending Functionality

Here is a step-by-step guide to implement event sending functionality in your JavaScript application:

  • Install the Azure Event Hubs JavaScript client library:

bash

npm install @azure/event-hubs

  • Create an instance of the EventHubClient:
    javascript
    const { EventHubClient } = require('@azure/event-hubs');
    
    const eventHubClient = new EventHubClient(
      'https://<event_hub_name>.servicebus.windows.net/<event_hub_name>',
      '<shared_access_key_name>',
      '<shared_access_key_value>'
    );
  • Send an event to the event hub:
    javascript
    eventHubClient.send({
      body: 'Hello, World!',
      partitionKey: 'my_partition_key'
    });

Get started for FREE with OpenObserve

Code examples for both authentication methods

Here are the code examples for both passwordless authentication and connection string methods:

  • Passwordless Authentication
    javascript
    const { EventHubClient } = require('@azure/event-hubs');
    const { DefaultAzureCredential } = require('@azure/identity');
    
    const credential = new DefaultAzureCredential();
    const eventHubClient = new EventHubClient(
      'https://<event_hub_name>.servicebus.windows.net/<event_hub_name>',
      credential
    );
    
    eventHubClient.send({
      body: 'Hello, World!',
      partitionKey: 'my_partition_key'
    });
    
    • Connection String Authentication
      javascript
      const { EventHubClient } = require('@azure/event-hubs');
      
      const connectionStr = 'Endpoint=sb://<event_hub_name>.servicebus.windows.net/;SharedAccessKeyName=<shared_access_key_name>;SharedAccessKey=<shared_access_key_value>;SharedAccessPolicyName=<shared_access_policy_name>';
      const eventHubClient = new EventHubClient(connectionStr);
      
      eventHubClient.send({
        body: 'Hello, World!',
        partitionKey: 'my_partition_key'

});

These code examples demonstrate how to send events to Azure Event Hubs using both passwordless authentication and connection string methods. Now, lets see the codes used for receiving events.

Receiving Events from Azure Event Hubs

Using Azure Blob Storage for Event Checkpointing

Azure Blob storage can be used for event checkpointing in Azure Event Hubs. This involves storing the last processed event in a blob container to ensure that events are not lost in case of a failure. Here is an example of how to use Azure Blob storage for event checkpointing:

csharp

using Azure.Storage.Blobs;
  • Create a blob client
    BlobClient blobClient = new BlobClient("https://<storage_account_name>.blob.core.windows.net/<blob_container_name>", new DefaultAzureCredential());
  • Create a blob to store the checkpoint
    BlobUploadOptions uploadOptions = new BlobUploadOptions();
    uploadOptions.BlobContentType = "application/octet-stream";
  • Upload the checkpoint to the blob
    await blobClient.UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes(checkpoint)), uploadOptions);

Get started for FREE with OpenObserve

Procedure to Configure an Azure Storage Account and Blob Container

To configure an Azure storage account and blob container for event checkpointing, follow these steps:

  1. Create an Azure storage account:
    • Go to the Azure portal and navigate to the Storage accounts section.
    • Click on + Create and enter the required details.
    • Select Blob storage as the storage type.
    • Click on Create to create the storage account.
  2. Create a blob container:
    • Go to the Azure portal and navigate to the Storage accounts section.
    • Select the storage account you created.
    • Click on Containers and then click on + Container.
    • Enter the name of the container and click on Create.

Instructions to authenticate and receive events

To authenticate and receive events from Azure Event Hubs, follow these steps. The language used is chsarp.

  • Install the Azure Event Hubs NuGet package:

Install-Package Azure.Messaging.EventHubs

  • Create an event hub client:
    EventHubClient eventHubClient = new EventHubClient("https://<event_hub_name>.servicebus.windows.net/<event_hub_name>", new DefaultAzureCredential());
  • Create an event processor client:
    EventProcessorClient eventProcessorClient = new EventProcessorClient(eventHubClient, EventHubConsumerClient.DefaultConsumerGroupName, "<event_hub_name>.servicebus.windows.net", "<event_hub_name>", new DefaultAzureCredential());
  • Register handlers for processing events and handling errors:
    eventProcessorClient.ProcessEventAsync += ProcessEventHandler;
    eventProcessorClient.ProcessErrorAsync += ProcessErrorHandler;
  • Start the processing:
    await eventProcessorClient.StartProcessingAsync();

Code demonstration for event reception

Here is a code example that demonstrates event reception from Azure Event Hubs:

using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Storage.Blobs;
  • Create an event hub client
    EventHubClient eventHubClient = new EventHubClient("https://<event_hub_name>.servicebus.windows.net/<event_hub_name>", new DefaultAzureCredential());
  • Create an event processor client
    EventProcessorClient eventProcessorClient = new EventProcessorClient(eventHubClient, EventHubConsumerClient.DefaultConsumerGroupName, "<event_hub_name>.servicebus.windows.net", "<event_hub_name>", new DefaultAzureCredential());
  • Register handlers for processing events and handling errors
    eventProcessorClient.ProcessEventAsync += ProcessEventHandler;
    eventProcessorClient.ProcessErrorAsync += ProcessErrorHandler;
  • Start the processing
    await eventProcessorClient.StartProcessingAsync();
  • Process event handler
    async Task ProcessEventHandler(ProcessEventArgs eventArgs)
    {
  • Write the body of the event to the console window
        Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));
  • Return a task to indicate that the event has been processed
      return Task.CompletedTask;
    }
  • Error handler
    async Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
    {
  • Write details about the error to the console window
  Console.WriteLine($"\tPartition '{eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.");
    Console.WriteLine(eventArgs.Exception.Message);
  • Return a task to indicate that the error has been handled
     return Task.CompletedTask;
    }

This code example demonstrates how to receive events from Azure Event Hubs using the EventProcessorClient class. It also shows how to handle events and errors using the ProcessEventAsync and ProcessErrorAsync event handlers.

Get started for FREE with OpenObserve

Send or receive events using JavaScript - Azure Event Hubs

Now, let’s see how OpenObserve can help you in your journey.

How Can OpenObserve Help in Using JavaScript to Access Azure Event Hubs Resources?

OpenObserve can help in using JavaScript to access Azure Event Hubs resources by providing a comprehensive set of tools and libraries for managing and processing events in Azure Event Hubs. Here are some ways OpenObserve can assist:

  • Event Hubs Client Library: OpenObserve provides a JavaScript client library for Azure Event Hubs that allows you to send and receive events in your Node.js application. The library includes features such as event processing, error handling, and partition management.
  • Event Hubs Browser Samples: OpenObserve provides browser samples for Azure Event Hubs that demonstrate how to use the JavaScript client library to send and receive events in the browser while authenticating with Azure Active Directory.
  • Event Hubs Resource Management: OpenObserve provides libraries for managing Azure Event Hubs resources via the Azure Resource Manager. This includes packages for creating and managing Event Hubs instances, consumer groups, and partitions.
  • Event Hubs Data Access: OpenObserve provides libraries for sending and receiving events from Azure Event Hubs instances. This includes packages for processing events from multiple partitions, balancing partition load across multiple instances, and using Azure Storage Blob to store checkpoints.
  • Event Hubs Authentication: OpenObserve provides libraries for authenticating with Azure Active Directory to access Azure Event Hubs resources. This includes packages for retrieving credentials using the InteractiveBrowserCredential from @azure/identity and setting up authentication properties in the configuration file.
  • Event Hubs Troubleshooting: OpenObserve provides troubleshooting guides for common issues such as authentication errors, event processing errors, and partition management errors.

Get started for FREE with OpenObserve

By using OpenObserve, you can simplify the process of accessing and managing Azure Event Hubs resources in your JavaScript applications, ensuring efficient and reliable event processing and data analysis.

Here are the relevant links to explore the OpenObserve platform:

Conclusion

This guide has equipped you with the knowledge to conquer the real-time data stream and unlock the potential of JavaScript and Azure Event Hubs for your web applications!

Get started for FREE with OpenObserve

Author:

authorImage

The OpenObserve Team comprises dedicated professionals committed to revolutionizing system observability through their innovative platform, OpenObserve. Dedicated to streamlining data observation and system monitoring, offering high performance and cost-effective solutions for diverse use cases.

OpenObserve Inc. © 2024