Azure Event Hubs client library samples for JavaScript

These sample programs show how to use the JavaScript client libraries for Azure Event Hubs in some common scenarios.

File Name Description
sendEvents.js demonstrates how the send() function can be used to send events to Event Hubs
useWithIotHub.js demonstrates how to use the EventHubClient with an IotHub instance
websockets.js demonstrates how to use WebSockets enable Event Hubs to work over an HTTP proxy and in environments where the standard AMQP port 5671 is blocked (see the sample for more information)
usingAadAuth.js demonstrates how to instantiate EventHubsClient using AAD token credentials obtained from using Service Principal Secrets
receiveEvents.js demonstrates how to use the EventHubConsumerClient to process events from all partitions of a consumer group in an Event Hubs instance.

Note: EventHubConsumerClient supports checkpointing using the @azure/eventhubs-checkpointstore-blob and @azure/storage-blob packages. For an example that demonstrates how to use this functionality, see the receiveEventsUsingCheckpointStore sample in the @azure/eventhubs-checkpointstore-blob repository.


The samples are compatible with Node.js >= 8.0.0.

You need an Azure subscription and an Azure Event Hub resource to run these sample programs. The IOT Hub sample additionally requires an IOT Hub resource. Samples retrieve credentials to access the event hub from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.

Adapting the samples to run in the browser requires some additional consideration. For details, please see the package README.


To run the samples using the published version of the package:

  1. Install the dependencies using npm:
npm install
  1. Compile the samples
npm run build
  1. Edit the file sample.env, adding the correct credentials to access the Azure service and run the samples. Then rename the file from sample.env to just .env. The sample programs will read this file automatically.

  2. Run whichever samples you like (note that some samples may require additional setup, see the table above):

node dist/sendEvents.js

Alternatively, run a single sample with the correct environment variables set (step 3 is not required if you do this), for example (cross-platform):

npx cross-env EVENTHUB_NAME="<hub name>" EVENTHUB_CONNECTION_STRING="<connection string>" node dist/sendEvents.js

Next Steps

Take a look at our API Documentation for more information about the APIs that are available in the clients.