
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@azure/event-hubs
Advanced tools
Azure Event Hubs is a scalable event processing service that ingests and processes large volumes of events and data, with low latency and high reliability.
Use the client library for Azure Event Hubs in your Node.js application to
Source code | Package (npm) | API Reference Documentation | Product documentation
Install the Azure Event Hubs client library using npm
npm install @azure/event-hubs
Prerequisites: You must have an Azure subscription and a Event Hubs Namespace to use this package. If you are using this package in a Node.js application, then use Node.js 6.x or higher.
TypeScript users need to have Node type definitions installed:
npm install @types/node
You also need to enable compilerOptions.allowSyntheticDefaultImports
in your tsconfig.json. Note that if you have enabled compilerOptions.esModuleInterop
, allowSyntheticDefaultImports
is enabled by default. See TypeScript's compiler options handbook for more information.
Interaction with Event Hubs starts with an instance of the EventHubClient class. You can instantiate this class using one of the below static methods on it
name-of-event-hub-instance.servicebus.windows.net
.name-of-event-hub-instance.servicebus.windows.net
.The following sections provide code snippets that cover some of the common tasks using Azure Event Hubs
Once you have created an instance of an EventHubClient
class, send events
using the send function.
You can also use the sendBatch method to send multiple events using a single call.
const client = EventHubClient.createFromConnectionString("connectionString", "eventHubName");
await client.send({ body: "my-event-body" });
await client.sendBatch(
[
{ body: "my-event-body-1" },
{ body: "my-event-body-2" },
{ body: "my-event-body-3" }
]
);
To send events to a particular partition, use the optional parameter partitionId
on the send
and sendBatch
functions.
You can use the getPartitionIds
function to get the ids of all available partitions in your Event Hub instance.
Note: When working with Azure Stream Analytics, the body of the event being sent should be a JSON object as well.
For example: body: { "message": "Hello World" }
Once you have created an instance of an EventHubClient
class, you can receive events in one of 2 ways:
Both ways require you to know the id of the partition that you want to receive events from. You can use the getPartitionIds function to get the ids of all available partitions in your Event Hub instance.
Use the receiveBatch function which returns a promise that resolves to an array of events.
This function takes an optional parameter called options
of type ReceiveOptions
which you can use to specify the Consumer Group you want to target or the position from where you want to start receiving events.
const client = EventHubClient.createFromConnectionString("connectionString", "eventHubName");
const myEvents = await client.receiveBatch("my-partitionId", 10);
Use the receive to set up event handlers and have it running as long as you need.
This function takes an optional parameter called options
of type ReceiveOptions
which you can use to specify the Consumer Group you want to target or the position from where you want to start receiving events.
const myEventHandler = event => {
// your code here
};
const myErrorHandler = error => {
// your error handler here
};
const receiveHandler = client.receive("partitionId", myEventHandler, myErrorHandler);
// When ready to stop receiving
await receiveHandler.stop();
You can use EventHubClient
to work with IotHub as well. This is useful for receiving telemetry data of IotHub from the linked EventHub.
Most likely the associated connection string will not have send claims. Hence getting HubRuntimeInfo or PartitionRuntimeInfo and receiving events would be the possible operations.
const client = await EventHubClient.createFromIotHubConnectionString("connectionString");
await client.getHubRuntimeInformation();
await client.getPartitionInformation("partitionId");
Notes: For scalable and efficient receiving, please take a look at azure-event-processor-host. The Event Processor host, internally uses the streaming receiver to receive events.
You can set the following environment variable to get the debug logs when using this library.
export DEBUG=azure*
export DEBUG=azure*,rhea*
DEBUG
environment variable as follows:export DEBUG=azure*,rhea*,-rhea:raw,-rhea:message,-azure:amqp-common:datatransformer
DEBUG
environment variable as follows:export DEBUG=azure:event-hubs:error,azure-amqp-common:error,rhea-promise:error,rhea:events,rhea:frames,rhea:io,rhea:flow
DEBUG
environment variable as shown above and then run your test script as follows:
out.log
and logging statements from the sdk go to debug.log
.
node your-test-script.js > out.log 2>debug.log
out.log
by redirecting stderr to stdout (&1), and then redirect stdout to a file:
node your-test-script.js >out.log 2>&1
out.log
.
node your-test-script.js &> out.log
Please take a look at the samples directory for detailed examples on how to use this library to send and receive events to/from Event Hubs.
FAQs
Azure Event Hubs SDK for JS.
We found that @azure/event-hubs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.