Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions sdk/eventhub/event-hubs/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The samples in this folder are for version 5.0.0 and above of this library. If y

Run the below in your samples folder to install the npm package for Event Hubs library.
```bash
npm install @azure/event-hubs@next
npm install
```

## Get connection string & Event Hubs name
Expand All @@ -20,35 +20,24 @@ npm install @azure/event-hubs@next

Before running a sample, update it with the connection string and the Event Hub name you have noted above.

## Running a sample
## Running samples

Start by copying the sample into a npm project.
```bash
mkdir event-hubs-samples
cd event-hubs-samples
npm init
# copy sample into this directory
```

If you don't have Typescript installed, then use `npm` to install it first.
```bash
npm install -g typescript
```

Install the `@azure/event-hubs@next` package into your project, as well as any other dependencies you might need.
```bash
npm install --save @azure/event-hubs@next
```
1. Copy this folder to your machine.
2. Install dependencies for the samples project.

```bash
npm install
```

One way to run Typescript samples is to use `ts-node`. To install `ts-node`, run the below in your sample folder
```bash
npm install ts-node
```
3. Rename the `sample.env` file to `.env`
4. Open the `.env` file in a text editor and fill in values related to the
sample you'd like to run.
5. Run the samples using ts-node:

Use `ts-node` to run the sample copied previously.
```bash
ts-node sample.ts
```
For example, to run the `sendEvents.ts` sample:

```bash
npx ts-node sendEvents.ts
```

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Feventhub%2Fevent-hubs%2Fsamples%2FREADME.png)
23 changes: 23 additions & 0 deletions sdk/eventhub/event-hubs/samples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "eventhubs-samples",
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
"version": "1.0.0",
"description": "The samples in this folder are for version 5.0.0 and above of this library. If you are using version 2.1.0 or lower, then please use [samples for v2.1.0](https://github.com/Azure/azure-sdk-for-js/tree/%40azure/event-hubs_2.1.0/sdk/eventhub/event-hubs/samples) instead",
"main": "dist/runsamples.js",
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
"scripts": {
"test": "ts-node ./runsamples.ts",
"build": "tsc -p ."
},
"keywords": [],
"author": "",
"license": "ISC",
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
"devDependencies": {
"ts-node": "^8.5.4",
"typescript": "^3.7.2"
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
},
"dependencies": {
Comment thread
richardpark-msft marked this conversation as resolved.
"@azure/core-amqp": "next",
Comment thread
richardpark-msft marked this conversation as resolved.
"@azure/event-hubs": "next",
"@azure/eventhubs-checkpointstore-blob": "next",
"rhea-promise": "^1.0.0"
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
}
}
21 changes: 14 additions & 7 deletions sdk/eventhub/event-hubs/samples/receiveEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import {
EventHubConsumerClient
} from "@azure/event-hubs";

const connectionString = "";
const eventHubName = "";
const consumerGroup = "";
const connectionString = process.env["EVENTHUB_CONNECTION_STRING"] || "";
const eventHubName = process.env["EVENTHUB_NAME"] || "";
const consumerGroup = process.env["CONSUMER_GROUP_NAME"] || "";

async function main() {
export async function main() {
console.log(`Running receiveEvents sample`);

const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);

const subscription = consumerClient.subscribe({
Expand All @@ -45,8 +47,13 @@ async function main() {
resolve();
}, 30000);
});

Comment thread
chradek marked this conversation as resolved.
console.log(`Exiting receiveEvents sample`);
}

main().catch((err) => {
console.log("Error occurred: ", err);
});
if (!process.env["RUNNING_IN_TESTS"]) {
Comment thread
richardpark-msft marked this conversation as resolved.
Outdated
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ import {
import { ContainerClient } from "@azure/storage-blob";
import { BlobCheckpointStore } from "@azure/eventhubs-checkpointstore-blob";

const connectionString = "";
const eventHubName = "";
const storageConnectionString = "";
const containerName = "";
const consumerGroup = "";
const connectionString = process.env["EVENTHUB_CONNECTION_STRING"] || "";
const eventHubName = process.env["EVENTHUB_NAME"] || "";
const consumerGroup = process.env["CONSUMER_GROUP_NAME"] || "";
const storageConnectionString = process.env["STORAGE_CONNECTION_STRING"] || "";
const containerName = process.env["STORAGE_CONTAINER_NAME"] || "";

export async function main() {
console.log(`Running receiveEventsUsingCheckpointStore sample`);

async function main() {
// this client will be used by our eventhubs-checkpointstore-blob, which
// persists any checkpoints from this session in Azure Storage
const containerClient = new ContainerClient(storageConnectionString, containerName);

if (!containerClient.exists()) {
if (!(await containerClient.exists())) {
await containerClient.create();
}

const checkpointStore : CheckpointStore = new BlobCheckpointStore(containerClient);


const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName, checkpointStore);

const subscription = consumerClient.subscribe({
Expand Down Expand Up @@ -75,8 +76,13 @@ async function main() {
resolve();
}, 30000);
});

console.log(`Exiting receiveEventsUsingCheckpointStore sample`);
}

main().catch((err) => {
console.log("Error occurred: ", err);
});
if (!process.env["RUNNING_IN_TESTS"]) {
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}
23 changes: 23 additions & 0 deletions sdk/eventhub/event-hubs/samples/runsamples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as dotenv from "dotenv";
Comment thread
richardpark-msft marked this conversation as resolved.
dotenv.config();
Comment thread
richardpark-msft marked this conversation as resolved.

import { main as sendEventsMain } from "./sendEvents";
import { main as receiveEventsMain } from "./receiveEvents";
import { main as receiveEventsUsingCheckpointStoreMain } from "./receiveEventsUsingCheckpointStore";
import { main as useWithIotHubMain } from "./useWithIotHub";
import { main as usingAadAuthMain } from "./usingAadAuth";
import { main as websocketsMain } from "./websockets";

async function main() {
Comment thread
richardpark-msft marked this conversation as resolved.
await sendEventsMain();
await receiveEventsMain();
await receiveEventsUsingCheckpointStoreMain();
await useWithIotHubMain();
await usingAadAuthMain();
await websocketsMain();
}

main().catch(err => {
console.log(err);
process.exit(1);
})
28 changes: 28 additions & 0 deletions sdk/eventhub/event-hubs/samples/sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Used in most samples
EVENTHUB_CONNECTION_STRING=<connection string WITHOUT an EntityPath>
EVENTHUB_NAME=<name of a single eventhub>
EVENTHUB_FQDN=<your-eventhubs-namespace>.servicebus.windows.net
CONSUMER_GROUP_NAME=<name of a consumer group>

# Used in the receiveEventsUsingCheckpointStore.ts sample
STORAGE_CONTAINER_NAME=<storage account connection string for checkpoints>

# Used in the useWithIotHub.ts sample
IOTHUB_CONNECTION_STRING=<connection string with EntityPath>

# Used in the usingAadAuth.ts sample
# Setup :
# Please ensure that your Azure Event Hubs resource is in US East, US East 2, or West Europe
# region. AAD Role Based Access Control is not supported in other regions yet.
# Register a new application in AAD and assign the "Azure Event Hubs Data Owner (Preview)" role to it
# - See https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app
# to register a new application in the Azure Active Directory.
# - Note down the CLIENT_ID and TENANT_ID from the above step.
# - In the "Certificates & Secrets" tab, create a secret and note that down.
# - In the Azure portal, go to your Even Hubs resource and click on the Access control (IAM)
# tab. Here, assign the "Azure Event Hubs Data Owner (Preview)" role to the registered application.
# - For more information on Event Hubs RBAC setup, learn more at
# https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-role-based-access-control)
AZURE_CLIENT_ID=
AZURE_TENANT_ID=
AZURE_CLIENT_SECRET=
19 changes: 13 additions & 6 deletions sdk/eventhub/event-hubs/samples/sendEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import { EventHubProducerClient } from "@azure/event-hubs";

// Define connection string and related Event Hubs entity name here
const connectionString = "";
const eventHubName = "";
const connectionString = process.env["EVENTHUB_CONNECTION_STRING"] || "";
const eventHubName = process.env["EVENTHUB_NAME"] || "";

export async function main(): Promise<void> {
console.log(`Running sendEvents sample`);

async function main(): Promise<void> {
const producer = new EventHubProducerClient(connectionString, eventHubName);

console.log("Creating and sending a batch of events...");
Expand Down Expand Up @@ -50,8 +52,13 @@ async function main(): Promise<void> {
}

await producer.close();
console.log(`Exiting sendEvents sample`);
}

if (!process.env["RUNNING_IN_TESTS"]) {
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}

main().catch((err) => {
console.log("Error occurred: ", err);
});
17 changes: 11 additions & 6 deletions sdk/eventhub/event-hubs/samples/useWithIotHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import { EventHubConsumerClient } from "@azure/event-hubs";
// Define IoT Hub Event Hubs-compatible connection string here.
// To find the correct connection string to use, visit:
// https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-read-builtin
const connectionString = "";
const consumerGroup = "";
const connectionString = process.env["IOTHUB_CONNECTION_STRING"] || "";
const consumerGroup = process.env["CONSUMER_GROUP_NAME"] || "";

async function main(): Promise<void> {
export async function main(): Promise<void> {
console.log(`Running useWithIotHub sample`);
const client = new EventHubConsumerClient(consumerGroup, connectionString);
/*
Refer to other samples, and place your code here to receive events using the above client.
Please note that send operations are not supported when this client is used against an IotHub instance
*/
await client.close();
console.log(`Exiting useWithIotHub sample`);
}

main().catch((err) => {
console.log("Error occurred: ", err);
});
if (!process.env["RUNNING_IN_TESTS"]) {
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}
23 changes: 15 additions & 8 deletions sdk/eventhub/event-hubs/samples/usingAadAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ import { EventHubConsumerClient } from "@azure/event-hubs";
import { DefaultAzureCredential } from "@azure/identity";

// Define Event Hubs Endpoint and related entity name here here
const evenHubsEndpoint = ""; // <your-eventhubs-namespace>.servicebus.windows.net
const eventHubName = "";
const consumerGroup = "";
const eventHubsFullyQualifiedName = process.env["EVENTHUB_FQDN"] || ""; // <your-eventhubs-namespace>.servicebus.windows.net
const eventHubName = process.env["EVENTHUB_NAME"] || "";
const consumerGroup = process.env["CONSUMER_GROUP_NAME"] || "";

// Define AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET of your AAD application in your environment

async function main(): Promise<void> {
export async function main(): Promise<void> {
console.log(`Running usingAadAuth sample`);

const credential = new DefaultAzureCredential();
const client = new EventHubConsumerClient(consumerGroup, evenHubsEndpoint, eventHubName, credential);
const client = new EventHubConsumerClient(consumerGroup, eventHubsFullyQualifiedName, eventHubName, credential);
/*
Refer to other samples, and place your code here
to send/receive events
*/
await client.close();

console.log(`Exiting usingAadAuth sample`);
}

main().catch((err) => {
console.log("error: ", err);
});
if (!process.env["RUNNING_IN_TESTS"]) {
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}
21 changes: 14 additions & 7 deletions sdk/eventhub/event-hubs/samples/websockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const url = require("url");
const httpsProxyAgent = require("https-proxy-agent");

// Define connection string and related Event Hubs entity name here
const connectionString = "";
const eventHubName = "";
const consumerGroup = "";
const connectionString = process.env["EVENTHUB_CONNECTION_STRING"] || "";
const eventHubName = process.env["EVENTHUB_NAME"] || "";
const consumerGroup = process.env["CONSUMER_GROUP_NAME"] || "";

// Create an instance of the `HttpsProxyAgent` class with the proxy server information like
// proxy url, username and password
Expand All @@ -31,7 +31,9 @@ const urlParts = url.parse("http://localhost:3128");
urlParts.auth = "username:password"; // Skip this if proxy server does not need authentication.
const proxyAgent = new httpsProxyAgent(urlParts);

async function main(): Promise<void> {
export async function main(): Promise<void> {
console.log(`Running websockets sample`);

const client = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName, {
webSocketOptions: {
webSocket: WebSocket,
Expand All @@ -42,8 +44,13 @@ async function main(): Promise<void> {
Refer to other samples, and place your code here to send/receive events
*/
await client.close();

console.log(`Exiting websockets sample`);
}

main().catch(err => {
console.log("error: ", err);
});
if (!process.env["RUNNING_IN_TESTS"]) {
main().catch((err) => {
console.log("Error occurred: ", err);
process.exit(1);
});
}