-
-
Save jeremymeng/022ec05e362cdbc8be2cefb68feb870c to your computer and use it in GitHub Desktop.
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> |
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
-
Can I use this method on my angular app without touching the server-side or azure configurations?
-
Does Use this method prevents me from the security issue on connectionString?
Kindly help...
@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.
@jeremymeng Ok. Got it to work. Thanks for the response.
@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.
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.
After
npm install
just runnpx parcel test.html