Skip to content

Commit 2c78d64

Browse files
authored
Merge pull request #189373 from spelluru/ehubjsqs022
fixed the issue with reading from start position
2 parents 87b612c + b7189dd commit 2c78d64

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

articles/event-hubs/event-hubs-node-get-started-send.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Send or receive events from Azure Event Hubs using JavaScript (latest)
33
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.
44
ms.topic: quickstart
5-
ms.date: 09/16/2021
5+
ms.date: 02/22/2022
66
ms.devlang: javascript
77
ms.custom: devx-track-js, mode-api
88
---
@@ -88,9 +88,9 @@ In this section, you create a JavaScript application that sends events to an eve
8888
* `EVENT HUBS NAMESPACE CONNECTION STRING`
8989
* `EVENT HUB NAME`
9090
1. Run `node send.js` to execute this file. This command sends a batch of three events to your event hub.
91-
1. In the Azure portal, verify that the event hub has received the messages. In the **Metrics** section, switch to **Messages** view. Refresh the page to update the chart. It might take a few seconds for it to show that the messages have been received.
91+
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.
9292

93-
[![Verify that the event hub received the messages](./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png)](./media/getstarted-dotnet-standard-send-v2/verify-messages-portal.png#lightbox)
93+
[![Verify that the event hub received the messages](./media/node-get-started-send/verify-messages-portal.png)](./media/node-get-started-send/verify-messages-portal.png#lightbox)
9494

9595
> [!NOTE]
9696
> For the complete source code, including additional informational comments, go to the [GitHub sendEvents.js page](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/eventhub/event-hubs/samples/v5/javascript/sendEvents.js).
@@ -122,45 +122,46 @@ Be sure to record the connection string and container name for later use in the
122122
1. Create a file called *receive.js*, and paste the following code into it:
123123
124124
```javascript
125-
const { EventHubConsumerClient } = require("@azure/event-hubs");
125+
const { EventHubConsumerClient, earliestEventPosition } = require("@azure/event-hubs");
126126
const { ContainerClient } = require("@azure/storage-blob");
127127
const { BlobCheckpointStore } = require("@azure/eventhubs-checkpointstore-blob");
128-
128+
129129
const connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
130130
const eventHubName = "EVENT HUB NAME";
131131
const consumerGroup = "$Default"; // name of the default consumer group
132132
const storageConnectionString = "AZURE STORAGE CONNECTION STRING";
133133
const containerName = "BLOB CONTAINER NAME";
134-
134+
135135
async function main() {
136136
// Create a blob container client and a blob checkpoint store using the client.
137137
const containerClient = new ContainerClient(storageConnectionString, containerName);
138138
const checkpointStore = new BlobCheckpointStore(containerClient);
139-
139+
140140
// Create a consumer client for the event hub by specifying the checkpoint store.
141141
const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName, checkpointStore);
142-
142+
143143
// Subscribe to the events, and specify handlers for processing the events and errors.
144144
const subscription = consumerClient.subscribe({
145145
processEvents: async (events, context) => {
146146
if (events.length === 0) {
147147
console.log(`No events received within wait time. Waiting for next interval`);
148148
return;
149149
}
150-
150+
151151
for (const event of events) {
152152
console.log(`Received event: '${event.body}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`);
153153
}
154154
// Update the checkpoint.
155155
await context.updateCheckpoint(events[events.length - 1]);
156156
},
157-
157+
158158
processError: async (err, context) => {
159159
console.log(`Error : ${err}`);
160160
}
161-
}
161+
},
162+
{ startPosition: earliestEventPosition }
162163
);
163-
164+
164165
// After 30 seconds, stop processing.
165166
await new Promise((resolve) => {
166167
setTimeout(async () => {
@@ -170,10 +171,10 @@ Be sure to record the connection string and container name for later use in the
170171
}, 30000);
171172
});
172173
}
173-
174+
174175
main().catch((err) => {
175176
console.log("Error occurred: ", err);
176-
});
177+
});
177178
```
178179
1. In the code, use real values to replace the following values:
179180
- `EVENT HUBS NAMESPACE CONNECTION STRING`
@@ -182,6 +183,12 @@ Be sure to record the connection string and container name for later use in the
182183
- `BLOB CONTAINER NAME`
183184
1. Run `node receive.js` in a command prompt to execute this file. The window should display messages about received events.
184185
186+
```
187+
C:\Self Study\Event Hubs\JavaScript>node receive.js
188+
Received event: 'First event' from partition: '0' and consumer group: '$Default'
189+
Received event: 'Second event' from partition: '0' and consumer group: '$Default'
190+
Received event: 'Third event' from partition: '0' and consumer group: '$Default'
191+
```
185192
> [!NOTE]
186193
> For the complete source code, including additional informational comments, go to the [GitHub receiveEventsUsingCheckpointStore.js page](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/eventhub/eventhubs-checkpointstore-blob/samples/v1/javascript/receiveEventsUsingCheckpointStore.js).
187194
64.6 KB
Loading

0 commit comments

Comments
 (0)