Microsoft Azure Event Grid Publish/Consume Samples for Node.js
This contains Node.js samples for publishing events to Azure Event Grid and consuming events from Azure Event Grid.
These samples demonstrates the following features:
- How to publish events to Azure Event Grid.
- How to consume events delivered by Azure Event Grid.
The above two samples use the Event Grid data plane SDK (azure-eventgrid).
- If you don't already have it, get node.js. Install a version greater than 8.0.
- Visual Studio Code
- Azure Functions Extension in Visual Studio Code
Running the Samples
The following are the steps to run the data plane samples and see events flowing through Event Grid:
Clone this repository onto your local machine.
git clone email@example.com:Azure-Samples/event-grid-node-publish-consume-events.git
Install the dependencies
cd event-grid-node-publish-consume-events npm install
Create an Event Grid topic: You will need to first create an Event Grid topic. The steps are described at https://docs.microsoft.com/en-us/azure/event-grid/scripts/event-grid-cli-create-custom-topic. Make a note of the topic name and resource group name.
Publish an Azure function: In this step, we will be using the EventGridConsumer sample and publishing it as an Azure function. Here are the steps:
a. Follow the instructions here to publish an Azure Function - https://code.visualstudio.com/tutorials/functions-extension/getting-started
b. Once you have published Azure function, navigate to the newly published Function in Azure Portal.
c. Click on "Get Function URL" and copy the function URL.
d. Create an event subscription to the topic you created in step 1, and provide this Azure function as the endpoint for your event subscription. https://docs.microsoft.com/en-us/azure/event-grid/scripts/event-grid-cli-subscribe-custom-topic describes how to create an event subscription.
e. Navigate to the Function (you created in step 2) in Azure Portal.
f. Click on the "Logs" link at the bottom of the page.
Start publishing events: In this step, we will be using the EventGridPublisher sample to start publishing events to the EventGrid topic you created in step1. Here are the steps:
a. In index.js, replace the topic-endpoint and topic-key fields with the topic end point and the key of the topic respectively.
b. Run the application in Visual studio code to publish events to this topic.
Verify you received the events: In this step, we will be verifying that the events are delivered to your event subscription. Here are the steps:
a. In the Logs view of the Azure Function, verify that you can see the logs that show the receipt of the EventGridEvent.
(Any additional resources or related projects)