You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Send or receive events from Azure Event Hubs using JavaScript (latest)
3
-
description: This article provides a walkthrough for creating a JavaScript application that sends/receives events to/from Azure Event Hubs using the latest azure/event-hubs package.
2
+
title: Send or receive events from Azure Event Hubs using JavaScript
3
+
description: This article provides a walkthrough for creating a JavaScript application that sends/receives events to/from Azure Event Hubs.
# Send events to or receive events from event hubs by using JavaScript (azure/event-hubs)
11
-
This quickstart shows how to send events to and receive events from an event hub using the **azure/event-hubs**JavaScript package.
10
+
# Send events to or receive events from event hubs by using JavaScript
11
+
This quickstart shows how to send events to and receive events from an event hub using the **@azure/event-hubs**npm package.
12
12
13
13
14
14
## Prerequisites
@@ -17,35 +17,36 @@ If you are new to Azure Event Hubs, see [Event Hubs overview](event-hubs-about.m
17
17
To complete this quickstart, you need the following prerequisites:
18
18
19
19
-**Microsoft Azure subscription**. To use Azure services, including Azure Event Hubs, you need a subscription. If you don't have an existing Azure account, you can sign up for a [free trial](https://azure.microsoft.com/free/) or use your MSDN subscriber benefits when you [create an account](https://azure.microsoft.com).
20
-
- Node.js version 8.x or later. Download the latest [long-term support (LTS) version](https://nodejs.org).
20
+
- Node.js LTS. Download the latest [long-term support (LTS) version](https://nodejs.org).
21
21
- Visual Studio Code (recommended) or any other integrated development environment (IDE).
22
-
-An active Event Hubs namespace and event hub. To create them, do the following steps:
22
+
-**Create an Event Hubs namespace and an event hub**. The first step is to use the [Azure portal](https://portal.azure.com) to create a namespace of type Event Hubs, and obtain the management credentials your application needs to communicate with the event hub. To create a namespace and an event hub, follow the procedure in [this article](event-hubs-create.md).
23
23
24
-
1. In the [Azure portal](https://portal.azure.com), create a namespace of type *Event Hubs*, and then obtain the management credentials that your application needs to communicate with the event hub.
25
-
1. To create the namespace and event hub, follow the instructions at [Quickstart: Create an event hub by using the Azure portal](event-hubs-create.md).
26
-
1. Continue by following the instructions in this quickstart.
27
-
1. To get the connection string for your Event Hub namespace, follow the instructions in [Get connection string](event-hubs-get-connection-string.md#azure-portal). Record the connection string to use later in this quickstart.
28
-
-**Create an Event Hubs namespace and an event hub**. The first step is to use the [Azure portal](https://portal.azure.com) to create a namespace of type Event Hubs, and obtain the management credentials your application needs to communicate with the event hub. To create a namespace and an event hub, follow the procedure in [this article](event-hubs-create.md). Then, get the **connection string for the Event Hubs namespace** by following instructions from the article: [Get connection string](event-hubs-get-connection-string.md#azure-portal). You use the connection string later in this quickstart.
29
-
30
-
### Install the npm package
24
+
### Install the npm package(s) to send events
31
25
To install the [Node Package Manager (npm) package for Event Hubs](https://www.npmjs.com/package/@azure/event-hubs), open a command prompt that has *npm* in its path, change the directory
32
-
to the folder where you want to keep your samples, and then run this command:
26
+
to the folder where you want to keep your samples.
For the receiving side, you need to install two more packages. In this quickstart, you use Azure Blob storage to persist checkpoints so that the program doesn't read the events that it has already read. It performs metadata checkpoints on received messages at regular intervals in a blob. This approach makes it easy to continue receiving messages later from where you left off.
@@ -84,9 +134,9 @@ In this section, you create a JavaScript application that sends events to an eve
84
134
console.log("Error occurred: ", err);
85
135
});
86
136
```
87
-
1. In the code, use real values to replace the following:
88
-
*`EVENT HUBS NAMESPACE CONNECTION STRING`
89
-
*`EVENT HUB NAME`
137
+
138
+
---
139
+
90
140
1. Run `node send.js` to execute thisfile. This command sends a batch of three events to your event hub.
91
141
1. In the Azure portal, verify that the event hub has received the messages. Refresh the page to update the chart. It might take a few seconds for it to show that the messages have been received.
92
142
@@ -112,15 +162,136 @@ To create an Azure storage account and a blob container in it, do the following
112
162
113
163
1. [Create an Azure storage account](../storage/common/storage-account-create.md?tabs=azure-portal)
114
164
2. [Create a blob container in the storage account](../storage/blobs/storage-quickstart-blobs-portal.md#create-a-container)
115
-
3. [Get the connection string to the storage account](../storage/common/storage-configure-connection-string.md)
[Get the connection string to the storage account](../storage/common/storage-configure-connection-string.md)
174
+
175
+
Note the connection string and the container name. You'll use them in the receive code.
176
+
177
+
---
178
+
179
+
### Install the npm packages to receive events
180
+
181
+
For the receiving side, you need to install two more packages. Inthis quickstart, you use Azure Blob storage to persist checkpoints so that the program doesn't read the events that it has already read. It performs metadata checkpoints on received messages at regular intervals in a blob. This approach makes it easy to continue receiving messages later from where you left off.
When developing locally, make sure that the user account that connects to Azure Event Hubs has the correct permissions. You'll need the [Azure Event Hubs Data Owner](../../../articles/role-based-access-control/built-in-roles.md#azure-event-hubs-data-owner) role in order to send and receive messages. To assign yourself this role, you'll need the User Access Administrator role, or another role that includes the `Microsoft.Authorization/roleAssignments/write` action. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. Learn more about the available scopes for role assignments on the [scope overview](/azure/role-based-access-control/scope-overview) page.
14
+
15
+
The following example assigns the `Azure Event Hubs Data Owner` role to your user account, which provides full access to Azure Event Hubs resources. In a real scenario, follow the [Principle of Least Privilege](/azure/active-directory/develop/secure-least-privileged-access) to give users only the minimum permissions needed for a more secure production environment.
16
+
17
+
### Azure built-in roles for Azure Event Hubs
18
+
For Azure Event Hubs, the management of namespaces and all related resources through the Azure portal and the Azure resource management API is already protected using the Azure RBAC model. Azure provides the below Azure built-in roles for authorizing access to an Event Hubs namespace:
19
+
20
+
-[Azure Event Hubs Data Owner](../../../articles/role-based-access-control/built-in-roles.md#azure-event-hubs-data-owner): Enables data access to Event Hubs namespace and its entities (queues, topics, subscriptions, and filters)
21
+
-[Azure Event Hubs Data Sender](../../../articles/role-based-access-control/built-in-roles.md#azure-event-hubs-data-sender): Use this role to give the sender access to Event Hubs namespace and its entities.
22
+
-[Azure Event Hubs Data Receiver](../../../articles/role-based-access-control/built-in-roles.md#azure-event-hubs-data-receiver): Use this role to give the receiver access to Event Hubs namespace and its entities.
23
+
24
+
If you want to create a custom role, see [Rights required for Event Hubs operations](../../../articles/service-bus-messaging/service-bus-sas.md#rights-required-for-service-bus-operations).
25
+
26
+
> [!IMPORTANT]
27
+
> In most cases, it will take a minute or two for the role assignment to propagate in Azure. In rare cases, it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.
28
+
29
+
### [Azure portal](#tab/roles-azure-portal)
30
+
31
+
1. In the Azure portal, locate your Event Hubs namespace using the main search bar or left navigation.
32
+
33
+
2. On the overview page, select **Access control (IAM)** from the left-hand menu.
34
+
35
+
3. On the **Access control (IAM)** page, select the **Role assignments** tab.
36
+
37
+
4. Select **+ Add** from the top menu and then **Add role assignment** from the resulting drop-down menu.
38
+
39
+
:::image type="content" source="media/event-hub-assign-roles/add-role.png" alt-text="A screenshot showing how to assign a role.":::
40
+
41
+
5. Use the search box to filter the results to the desired role. For this example, search for `Azure Event Hubs Data Owner` and select the matching result. Then choose **Next**.
42
+
43
+
6. Under **Assign access to**, select **User, group, or service principal**, and then choose **+ Select members**.
44
+
45
+
7. In the dialog, search for your Azure AD username (usually your *user@domain* email address) and then choose **Select** at the bottom of the dialog.
46
+
47
+
8. Select **Review + assign** to go to the final page, and then **Review + assign** again to complete the process.
48
+
49
+
### [Azure CLI](#tab/roles-azure-cli)
50
+
51
+
To assign a role at the resource level using the Azure CLI, you first must retrieve the resource ID using the `az servicebus namespace show` command. You can filter the output properties using the `--query` parameter.
52
+
53
+
```azurecli
54
+
az servicebus namespace show -g '<your-event-hub-resource-group>' -n '<your-event-hub-name> --query id
55
+
```
56
+
57
+
Copy the output `Id` from the preceding command. You can then assign roles using the [az role](/cli/azure/role) command of the Azure CLI.
58
+
59
+
```azurecli
60
+
az role assignment create --assignee "<user@domain>" \
0 commit comments