Azure Event Hubs SDK

JavaScript Client Library

Introduction to the JavaScript SDK

The Azure Event Hubs JavaScript client library allows you to send and receive events from an Azure Event Hub. This SDK is designed for use in both Node.js and browser environments.

With this library, you can:

  • Publish events to Event Hubs.
  • Subscribe to events from Event Hubs.
  • Manage consumer groups.
  • Handle connection management and error scenarios gracefully.

Getting Started

Installation

Install the Event Hubs JavaScript SDK using npm:

npm install @azure/event-hubs

Basic Usage: Sending Events

Here's a simple example of how to send events using the SDK:


import { EventHubProducerClient } from "@azure/event-hubs";

async function sendEvents() {
    const connectionString = "YOUR_EVENTHUBS_CONNECTION_STRING"; // Replace with your connection string
    const eventHubName = "YOUR_EVENTHUB_NAME"; // Replace with your Event Hub name

    const producerClient = new EventHubProducerClient(connectionString, eventHubName);

    try {
        const batch = await producerClient.createBatch();
        batch.tryAdd({ body: "First event" });
        batch.tryAdd({ body: "Second event" });
        await producerClient.sendBatch(batch);
        console.log("Successfully sent events!");
    } finally {
        await producerClient.close();
    }
}

sendEvents().catch((err) => {
    console.error("Error sending events: ", err);
});
                    

For a live demonstration and more advanced examples, explore our JavaScript tutorials.

API Reference

Explore the full API documentation to understand the capabilities of the Event Hubs JavaScript SDK:

Key Classes:

  • EventHubProducerClient: For sending events.
  • EventHubConsumerClient: For receiving events.
  • ProducerClientOptions, ConsumerClientOptions: For configuring client behavior.
  • EventData: Represents an event being sent or received.

Advanced Topics

Dive deeper into specific features and patterns: