Quickstart: Produce events to Azure Event Hubs with JavaScript
This quickstart guide will walk you through the process of sending events to an Azure Event Hub using a simple JavaScript application.
Prerequisites
- An Azure subscription. If you don't have one, create a free account before you begin.
- An Azure Event Hubs namespace and an Event Hub. Follow the guide to create resources.
- Node.js installed on your development machine.
- Basic understanding of JavaScript and asynchronous programming.
Steps
Set up your project
Create a new directory for your project and navigate into it. Initialize a Node.js project:
mkdir event-hubs-producer
cd event-hubs-producer
npm init -y
Install the Azure Event Hubs SDK for JavaScript:
npm install @azure/event-hubs
Create the producer script
Create a new file named producer.js and add the following code. Replace the placeholder values with your Event Hubs connection string and hub name.
// Import necessary modules from the Azure Event Hubs SDK
const { EventHubProducerClient } = require("@azure/event-hubs");
// Replace with your actual Event Hubs connection string and hub name
const connectionString = "YOUR_EVENT_HUBS_CONNECTION_STRING";
const hubName = "YOUR_EVENT_HUB_NAME";
async function main() {
// Create a producer client to send events
const producerClient = new EventHubProducerClient(connectionString, hubName);
console.log("Starting to send events...");
try {
// Create a batch of events to send
const batch = await producerClient.createBatch();
// Add events to the batch
batch.tryAdd({ body: "Hello, Event Hubs!" });
batch.tryAdd({ body: { message: "This is a JSON event.", timestamp: new Date() } });
batch.tryAdd({ body: "Another event with custom properties.", properties: { priority: "high" } });
// Send the batch of events
await producerClient.sendBatch(batch);
console.log(`Successfully sent ${batch.eventDataCount} events.`);
// Example of sending events individually (less efficient for many events)
// await producerClient.sendEvent("Event 1");
// await producerClient.sendEvent({ body: "Event 2", sequenceNumber: 123 });
} catch (error) {
console.error("Error sending events:", error);
} finally {
// Close the producer client
await producerClient.close();
console.log("Producer client closed.");
}
}
main().catch((err) => {
console.error("The receive process encountered an error.", err);
});
Important: Securely manage your connection string. Avoid hardcoding it in production applications. Consider using environment variables or Azure Key Vault.
Run the producer
Open your terminal in the project directory and run the script:
node producer.js
You should see output indicating that events are being sent and the producer client is closing.
Verify events (Optional)
You can verify that events have been received by checking your Event Hubs instance in the Azure portal or by setting up a consumer application using the Azure Event Hubs SDK.
Next Steps
- Learn how to consume events from Azure Event Hubs.
- Explore advanced scenarios like partitioning and load balancing.
- Integrate Event Hubs with other Azure services like Azure Functions or Azure Stream Analytics.