Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
client.receive(
'1',
(eventData) => { // on 'message
if (eventData.annotations) {
const enqueuedTime = eventData.annotations['x-opt-enqueued-time'];
console.debug(`Received message from IoT Hub, enqueued at ${enqueuedTime}`);
} else {
console.debug(`Received message from IoT Hub`);
}
updateState(eventData.body);
},
(error) => {
console.error(`Error receiving message from Event Hubs: ${error}`);
},
{
eventPosition: EventPosition.fromEnqueuedTime(Date.now())
}
);
}
partitionIds.forEach(async (partitionId: string) => {
const receiveOptions = {
consumerGroup,
enableReceiverRuntimeMetric: true,
eventPosition: EventPosition.fromEnqueuedTime(startTime),
name: `${hubInfo.path}_${partitionId}`,
};
let receiver: ReceiveHandler;
try {
receiver = eventHubClient.receive(
partitionId,
onMessage,
(err: object) => {
console.log(err); // tslint:disable-line: no-console
},
receiveOptions);
receivers.push(receiver);
await delay(SERVER_WAIT).then(() => {
receiver.stop().catch(err => {
console.log(`couldn't stop receiver on partition[${partitionId}]: ${err}`); // tslint:disable-line: no-console
});
private static async SendAndReceiveEvents() {
console.log("creating consumer...");
const consumer = EventHubs.client.createConsumer(
EventHubClient.defaultConsumerGroupName,
EventHubs.partitionId[0],
EventPosition.fromEnqueuedTime(new Date())
);
console.log("sending events...");
const producerOptions = {
partitionId: EventHubs.partitionId[0]
};
const producer = EventHubs.client.createProducer(producerOptions);
await producer.send({ body: "JS Event Test 1" });
await producer.send({ body: "JS Event Test 2" });
await producer.send({ body: "JS Event Test 3" });
console.log("receiving events...");
let eventsReceived = await consumer.receiveBatch(3, 5);
eventsReceived.forEach((event) => {
console.log(`Event received: ${event.body}`);
});
partitionIds.forEach((partitionId) => {
ehClient.receive(partitionId, onEventHubMessage, onEventHubError, { eventPosition: EventPosition.fromEnqueuedTime(startAfterTime) });
});
return new Promise((resolve) => setTimeout(() => resolve(), 3000));
function generateReceivers(options, partitionIds) {
const { startTime, consumerGroup, receiveHandler, errorHandler } = options;
const receiveOptions = {
enqueuedTime: startTime,
eventPosition: EventPosition.fromEnqueuedTime(startTime),
consumerGroup: consumerGroup
};
return partitionIds.map(partitionId => {
return ehClient.receive(
partitionId,
receiveHandler,
errorHandler,
receiveOptions
);
});
}
partitionIds.forEach((partitionId) => {
this.outputLine(label, `Created partition receiver [${partitionId}] for consumerGroup [${consumerGroup}]`);
this._eventHubClient.receive(partitionId,
this.printMessage(this._outputChannel, label, deviceItem),
this.printError(this._outputChannel, label),
{
eventPosition: EventPosition.fromEnqueuedTime(startAfterTime),
consumerGroup,
});
});
}
return ids.map(function (id) {
return ehClient.receive(id, printMessage, printError, { eventPosition: EventPosition.fromEnqueuedTime(Date.now()) });
});
}).catch(printError);