Skip to content

Instantly share code, notes, and snippets.

@jeremymeng
Created April 8, 2022 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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>
@jeremymeng
Copy link
Author

After npm install just run npx parcel test.html

@chandimaei
Copy link

Does this require setting up the azure EventHub on the azure portal as well?

I came from here: Azure/azure-sdk-for-js#18797

having the same issue with azure service-bus implementing on Angular. I actually have two questions now,
letting you know that my server-side already implemented with service-bus

  1. Can I use this method on my angular app without touching the server-side or azure configurations?

  2. Does Use this method prevents me from the security issue on connectionString?

Kindly help...

@jeremymeng
Copy link
Author

jeremymeng commented Oct 6, 2022

@chandimaei I use Event Hubs to demonstrate browser bundling. It should be very similar for Service Bus and you don't need Azure Event Hub resource.

Can I use this method on my angular app without touching the server-side or azure configurations?

Yes this is for running in browsers.

Does Use this method prevents me from the security issue on connectionString?

No, this is for demonstration purpose so I use connection string directly in my code. This is not secure in production. You probably want to use SAS. For example, generate a short-lived SAS token in your backend/server-side and use that in the client to access Service Bus.

@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