Skip to content

This TypeScript/Node.js sample listens for change notifications from Microsoft Graph delivered to Azure Event Grid.

License

Notifications You must be signed in to change notification settings

microsoftgraph/msgraph-sample-eventgrid-notifications-typescript

Repository files navigation

Microsoft Graph Event Grid change notifications sample for Node.js

npm build License.

Subscribe for Microsoft Graph change notifications to be notified when your user's data changes, so you don't have to poll for changes.

This sample Node.js web application shows how to subscribe for change notifications to be delivered to Azure Event Grid.

This sample uses:

Prerequisites

Create Azure resources

In order to configure the sample, you'll need to do the following steps in the Azure portal.

Register an application

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.

  2. Select Microsoft Entra ID in the left-hand navigation, then select App registrations under Manage.

  3. Select New registration. Enter a name for your application, for example, Event Grid Change Notifications Sample.

  4. Set Supported account types to Accounts in this organizational directory only.

  5. Leave Redirect URI empty.

  6. Select Register. On the application's Overview page, copy the value of the Application (client) ID and Directory (tenant) ID and save them, you will need these values in the next step.

  7. Select API permissions under Manage.

  8. Remove the default User.Read permission under Configured permissions by selecting the ellipses (...) in its row and selecting Remove permission.

  9. Select Add a permission, then Microsoft Graph.

  10. Select Application permissions.

  11. Select User.Read.All, then select Add permissions.

  12. Select Grant admin consent for..., then select Yes to provide admin consent for the selected permission.

    A screenshot of configured permissions

  13. Select Certificates and secrets under Manage, then select New client secret.

  14. Enter a description, choose a duration, and select Add.

  15. Copy the secret from the Value column, you will need it in the next steps.

    [!IMPORTANT] This client secret is never shown again, so make sure you copy it now.

Create a resource group

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.

  2. Select Resource groups in the left-hand navigation, then select Create.

  3. Select the Azure subscription and region to create the resource group in, and provide a name for the resource group, then select Review + create.

  4. Once the validation completes, select Create.

Authorize Microsoft Graph to create partner topics

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.

  2. Search for Event Grid Partner Configurations and select it from the results.

    A screenshot of the search results in the Azure portal

  3. Select Create.

  4. Select the Azure subscription and the resource group you created in the previous step.

  5. Select Partner Authorization.

  6. Search for MicrosoftGraphAPI and select it. Select Add.

    A screenshot of the partner authorization

  7. Select Review + create. Once the validation completes, select Create.

Configure the sample

Create a new file named .env.development.local in the root of this project and add the following.

# The Application (client) ID from your app registration
CLIENT_ID=YOUR_CLIENT_ID
# The client secret from your app registration
CLIENT_SECRET=YOUR_CLIENT_SECRET
# The Directory (tenant) ID from your app registration
TENANT_ID=YOUR_TENANT_ID
# The ID of the Azure subscription where the Event Grid partner topic should be created
SUBSCRIPTION_ID=YOUR_AZURE_SUBSCRIPTION_ID
# The name of the Azure resource group where the Event Grid partner topic should be created
RESOURCE_GROUP=YOUR_RESOURCE_GROUP
# The name of the Event Grid partner topic to be created
EVENT_GRID_TOPIC=YOUR_EVENT_GRID_TOPIC
# The Azure location name (ex. eastus)
LOCATION=YOUR_AZURE_LOCATION
# The port number to listen on
PORT_NUMBER=3000

Set the values as follows.

Setting Value
CLIENT_ID The Application (client) ID from your app registration.
CLIENT_SECRET The client secret from your app registration.
TENANT_ID The Directory (tenant) ID from your app registration.
SUBSCRIPTION_ID The ID of your Azure subscription. This can be found in the Azure portal. Search for Subscriptions and select it from the results.
RESOURCE_GROUP The name of the resource group you created in the previous steps.
EVENT_GRID_TOPIC The name Microsoft Graph should use to create the partner topic, for example: EventGridNotifications.
LOCATION The location you created your resource group in. You can find this by running the following command in Cloud Shell in the Azure portal: az account list-locations. Use the name value for the location, for example: eastus.

Create a dev tunnel

A dev tunnel will allow Azure Event Grid to reach the sample running on your development machine.

  1. Run the following command to login to the dev tunnel service. You can login with either a Microsoft Azure Active Directory account, a Microsoft account, or a GitHub account.

    devtunnel user login
  2. Run the following commands to create a tunnel. Copy the Tunnel ID from the output.

    devtunnel create --allow-anonymous
  3. Run the following command to assign the sample's port (3000) to the tunnel. Replace tunnel-id with the Tunnel ID copied in the previous step.

    devtunnel port create tunnel-id -p 3000
  4. Run the following command to host the tunnel. Replace tunnel-id with the Tunnel ID copied in the previous step.

    devtunnel host tunnel-id
  5. Copy the URL labeled Connect via browser. Open this URL in your browser and select Continue to enable the tunnel.

Run the sample

  1. Run the sample by pressing F5 in Visual Studio or Visual Studio Code. Alternatively, you can use the dotnet run command.

  2. Monitor the output. When you see the following, proceed to the next step.

    Created new subscription with ID aac7509d-94a0-4d50-addd-8341e02da4ac
    Please activate the GraphUserNotifications partner topic in the Azure portal and create an event subscription.
  3. In the Azure portal, navigate to the resource group you created. It should contain a new Event Grid Partner Topic. Select this topic.

    A screenshot of the partner topic

  4. Select Activate.

  5. Select Event Subscription to add a new subscription.

  6. Provide a name for the subscription, then set Endpoint Type to Webhook.

  7. Select Configure an endpoint. Enter your dev tunnel URL. Select Confirm Selection.

  8. Select Create and wait for the deployment to succeed.

A screenshot of the Create Event Subscription page

Generate events

Using the Azure portal or the Microsoft admin center, add, update, or delete users. Watch the sample's output for the notifications.

Received Microsoft.Graph.UserUpdated notification from Event Grid
User Adele Vance (ID: 37a7dc1b-812b-4336-8210-76b2180b574a) was created or updated

Code of conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

About

This TypeScript/Node.js sample listens for change notifications from Microsoft Graph delivered to Azure Event Grid.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published