Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const consumerGroupName = "$Default";
const onReceivedEventsHandler: OnReceivedEvents = async (events, context) => {
for (const message of events) {
console.log(`Received event: ${message.body}`);
}
};
const subscription = client.subscribe(consumerGroupName, onReceivedEventsHandler,
// for simplicity we'll just target a single partition for our demo
partitionIds[0], {
onError: async (err: Error, partitionContext: PartitionContext) => {
console.log(`Error occurred in the subscription for ${partitionContext.partitionId}: ${err}`);
},
// if this subscription happens tob e the first
defaultEventPosition: EventPosition.earliest()
});
// Waiting long enough before closing the consumer to receive event
await delay(5000);
await subscription.close();
await client.close();
}
async function main(): Promise {
const client = new EventHubClient(connectionString, eventHubName);
const partitionIds = await client.getPartitionIds();
const consumer = client.createConsumer("$Default", partitionIds[0], EventPosition.earliest());
const onMessageHandler: OnMessage = (brokeredMessage: EventData) => {
console.log(`Received event: ${brokeredMessage.body}`);
};
const onErrorHandler: OnError = (err: MessagingError | Error) => {
console.log("Error occurred: ", err);
};
try {
const rcvHandler = consumer.receive(onMessageHandler, onErrorHandler);
// Waiting long enough before closing the consumer to receive event
await delay(5000);
await rcvHandler.stop();
} finally {
await client.close();
async function main(): Promise {
const client = new EventHubClient(connectionString, eventHubName);
const partitionIds = await client.getPartitionIds();
const consumer = client.createConsumer("$Default", partitionIds[0], EventPosition.earliest());
const batchSize = 1;
try {
for (let i = 0; i < 5; i++) {
const events = await consumer.receiveBatch(batchSize, 5);
if (!events.length) {
console.log("No more events to receive");
break;
}
console.log(`Received events: ${events.map(event => event.body)}`);
}
let iteratorCount = 0;
for await (const events of consumer.getEventIterator()) {
iteratorCount++;
console.log(`Received event: ${events.body}`);