Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main(): Promise {
const client = new EventHubClient(connectionString, eventHubName);
const partitionIds = await client.getPartitionIds();
const producer = client.createProducer({ partitionId: partitionIds[0] });
try {
// NOTE: For receiving events from Azure Stream Analytics, please send Events to an EventHub
// where the body is a JSON object/array.
// const events = [
// { body: { "message": "Hello World 1" }, applicationProperties: { id: "Some id" }, partitionKey: "pk786" },
// { body: { "message": "Hello World 2" } },
// { body: { "message": "Hello World 3" } }
// ];
console.log("Sending single event...");
const scientist = listOfScientists[0];
producer.send({ body: `${scientist.firstName} ${scientist.name}` });
console.log("Sending multiple events...");
const events: EventData[] = [];
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();
static async Run() {
console.log(EventHubs.dedent`
------------------------
"Event Hubs"
------------------------
1) Get partitions ID
2) Send a batch of 3 events
3) Get a batch of events
`);
let eventHubName = "myeventhub";
let connectionString = process.env["EVENT_HUBS_CONNECTION_STRING"] || "";
EventHubs.client = new EventHubClient(connectionString, eventHubName);
try {
await EventHubs.getPartitionsIds();
await EventHubs.SendAndReceiveEvents();
} finally {
//At the end the client must be closed.
await EventHubs.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()) {