azure-sdk-for-js/sdk/eventgrid/eventgrid-namespaces
Jeremy Meng a28e8f0795 [EngSys] move to vendored version of cross-env via dev-tool
***NO_CI***

- apply the transformation

- update samples' README
2024-11-02 00:48:06 +00:00
..
review [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
samples [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
samples-dev [EngSys] standardize OSS copyright header 2024-08-27 13:01:38 -07:00
src [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
test/public [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
.nycrc [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00
CHANGELOG.md Post release automated changes for eventgrid releases (#30002) 2024-06-12 13:27:52 -04:00
LICENSE [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00
README.md [@azure/eventgrid-system-events][@azure/eventgrid-namespaces] Rename eventSubscriptionName to subscriptionName, Remove topicName subscriptionName as optional, Update CI YML (#29977) 2024-06-10 13:23:16 -07:00
api-extractor.json [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00
assets.json [@azure/eventgrid-namespaces] GA Release for V1.0.0 (#29764) 2024-06-06 15:10:16 -07:00
eslint.config.mjs [eslint-plugin] upgrade typescript-eslint to v8 2024-08-23 10:19:23 -07:00
karma.conf.js [@azure/eventgrid-namespaces][@azure/eventgrid]Migrate EventGrid Packages away from Local Authentication (#30309) 2024-07-10 12:56:51 -07:00
package.json [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
sample.env [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00
test.env [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00
tests.yml [Eng] Set Default for Federated Auth to True (#30163) 2024-07-22 17:24:20 -07:00
tsconfig.json [EngSys] remove tsconfig.package.json 2024-07-16 13:27:25 +00:00
tsdoc.json [@azure/eventgrid-namespaces] - Release EG Namespaces SDK V1.0.0-beta.1 (#29245) 2024-04-11 18:26:24 -07:00

README.md

Azure Event Grid Namespaces client library for JavaScript

Azure Event Grid is a cloud-based service that provides reliable event delivery at massive scale.

Use the client library to Send events to Event Grid Namespaces

Key links:

Getting started

Currently supported environments

See our support policy for more details.

Prerequisites

If you use the Azure CLI, replace <your-resource-group-name> and <your-resource-name> with your own unique names:

Create an Event Grid Topic

az eventgrid topic create --location <location> --resource-group <your-resource-group-name> --name <your-resource-name>

Create an Event Grid Domain

az eventgrid domain create --location <location> --resource-group <your-resource-group-name> --name <your-resource-name>

Install the @azure/eventgrid-namespaces package

Install the Azure Event Grid Namespaces client library for JavaScript with npm:

npm install @azure/eventgrid-namespaces

Create and authenticate Namespace clients

To create a client object to access the Event Grid Namespaces API, you will need the endpoint of your Event Grid topic and a credential. The Event Grid Namespaces clients can use an Access Key.

You can find the endpoint for your Event Grid topic either in the Azure Portal or by using the Azure CLI snippet below:

az eventgrid topic show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"

Using an Access Key

Use the Azure Portal to browse to your Event Grid resource and retrieve an Access Key, or use the Azure CLI snippet below:

az eventgrid topic key list --resource-group <your-resource-group-name> --name <your-event-grid-topic-name>

Once you have an API key and endpoint, you can use the AzureKeyCredential class to authenticate the client as follows:

const { EventGridSenderClient, EventGridReceiverClient, AzureKeyCredential } = require("@azure/eventgrid-namespaces");

const eventGridSenderClient = new EventGridSenderClient(
  "<endpoint>",
  new AzureKeyCredential("<Access Key>")
);

const eventGridReceiverClient = new EventGridReceiverClient(
  "<endpoint>",
  new AzureKeyCredential("<Access Key>")
);

Azure EventGrid provides integration with Azure Active Directory (Azure AD) for identity-based authentication of requests. With Azure AD, you can use role-based access control (RBAC) to grant access to your Azure Event Grid resources to users, groups, or applications.

To send events to a topic or domain with a TokenCredential, the authenticated identity should have the "EventGrid Data Sender" role assigned.

With the @azure/identity package, you can seamlessly authorize requests in both development and production environments. To learn more about Azure Active Directory, see the @azure/identity README.

For example, use can use DefaultAzureCredential to construct a client which will authenticate using Azure Active Directory:

const { EventGridSenderClient, EventGridReceiverClient } = require("@azure/eventgrid-namespaces");
const { DefaultAzureCredential } = require("@azure/identity");

const eventGridSenderClient = new EventGridSenderClient(
  "<endpoint>",
  new DefaultAzureCredential(),
  "<topicName>"
);

const eventGridReceiverClient = new EventGridReceiverClient(
  "<endpoint>",
  new DefaultAzureCredential(),
  "<topicName>",
  "<subscriptionName>"
);

Key concepts

Sending and Receiving Events

EventGridSenderClient can be used for sending events to an Event Grid. You can initialize it as:

const eventGridSenderClient = new EventGridSenderClient(
  "<endpoint>",
  new AzureKeyCredential("<API Key>"),
  "<topicName>"
);

EventGridReceiverClient can be used for receiving events from an Event Grid. You can initialize it as:

const eventGridReceiverClient = new EventGridReceiverClient(
  "<endpoint>",
  new AzureKeyCredential("<API Key>"),
  "<topicName>",
  "<subscriptionName>"
);

Distributed Tracing and Cloud Events

This library supports distributed tracing using @azure/core-tracing. When using distributed tracing, this library will create a span during a send operation. In addition, when sending events using the Cloud Events 1.0 schema, the SDK will add distributed tracing metadata to the events using the Distributed Tracing extension. The values for the traceparent and tracestate extension properties correspond to the traceparent and tracestate headers from the HTTP request which sends the events. If an event already has a traceparent extension property it is not updated.

Event Grid on Kubernetes

This library has been tested and validated on Kubernetes using Azure Arc.

Examples

Publish an Event to an Event Grid Topic

const { EventGridSenderClient, AzureKeyCredential } = require("@azure/eventgrid-namespaces");

const client = new EventGridSenderClient(
  "<endpoint>",
  new AzureKeyCredential("<API key>"),
  "<topicName>"
);

const cloudEvent: CloudEvent = {
  type: "example",
  source: "https://example.com",
  id: `singleEventIdV210001`,
  time: new Date(),
  data: {
    resourceUri: "https://dummyurl.com",
  },
  specversion: "1.0",
};
// Publish the Cloud Event
await client.sendEvents(cloudEvent);

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

const { setLogLevel } = require("@azure/logger");

setLogLevel("info");

For more detailed instructions on how to enable the logs, you can look at the @azure/logger package docs.

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions