Skip to content

Instantly share code, notes, and snippets.

@jeremymeng
Created April 8, 2022 00:12
Show Gist options
  • Save jeremymeng/022ec05e362cdbc8be2cefb68feb870c to your computer and use it in GitHub Desktop.
Save jeremymeng/022ec05e362cdbc8be2cefb68feb870c to your computer and use it in GitHub Desktop.
@azure/event-hubs browser parcel bundler
const { EventHubProducerClient } = require("@azure/event-hubs");
require("@azure/logger").setLogLevel("verbose");
localStorage.debug= "azure*,rhea-promise:error,rhea:events,rhea:frames,rhea:io,rhea:flow"
function generateEvents(n, size) {
result = [];
for (let i = 0; i < n; i++) {
const obj = {
dataType: "data",
body: Buffer.alloc(size),
}
result.push(obj);
}
return result;
}
async function main() {
const connectionString = "<eventhubs connection string>";
const eventHubName = "hub1";
const events = generateEvents(10, 255 * 1024)
for (let i= 0; i < events.length; i++) {
console.log("##### sending event #" + i);
const client = new EventHubProducerClient(connectionString, eventHubName);
const batch = await client.createBatch();
const event = events[i];
const added = batch.tryAdd(event);
if (!added) {
console.error("failed to add event", i);
} else {
console.log("##### added event", i)
}
await client.sendBatch(batch);
}
}
console.clear();
console.log("start sendBatch testing...");
main().then("completed").catch(console.error);
{
"name": "lost-events",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@azure/event-hubs": "^5.7.0"
},
"devDependencies": {
"os-browserify": "^0.3.0",
"parcel": "^2.2.1",
"path-browserify": "^1.0.1"
},
"browserslist": "> 1%, not dead"
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Test</title>
</head>
<body>
<script src="https://unpkg.com/regenerator-runtime@0.13.9/runtime.js"></script>
<script type="module" src="./batchSend.js"></script>
</body>
</html>
@chandimaei
Copy link

@jeremymeng Ok. Got it to work. Thanks for the response.

@jeremymeng
Copy link
Author

@chandimaei Glad that it worked for you! Were there anything extra that you needed to do? If there's any, I can update my instructions to include those.

@chandimaei
Copy link

chandimaei commented Oct 8, 2022

Yeah. As I am running this on an Angular project, other than the things you mentioned I had to update the polyfills as well. Then it worked as a charm. Need to work on the security concern though. Should consider your suggestion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment