1

I am using node.js and the Microsoft Graph npm package (@microsoft/microsoft-graph-client)for calling webhook in office 365 for calendar events. I am receiving multiple webhook notifications for every Office 365 calendar event update, delete and create.

My source code is

router.post("/webhook", (req, res) => { 
  if (req.query.validationToken) {
    res.status(200);
    res.send(req.query.validationToken)
  } else {
    res.status(202);
    console.log(req.body.value[0].changeType);
    console.log(req.body.value[0].resource);
  }

});

//CREAE A WEBHOOK
router.get("/createWebhook", async (req, res) => {
  const accessToken = await authHelper.getAccessToken(req.cookies, res);
  const client = graph.Client.init({
    authProvider: (done) => {
      done(null, accessToken);
    }
  });

  const subscription = {
    changeType: "deleted,updated,created",
    notificationUrl: "https://abccb3e5.ngrok.io/calendar/webhook",
    resource: "me/events",
    expirationDateTime: "2020-01-26T18:23:45.9356913Z",
    clientState: "My calendar sync"
  };
  try {
       client.api('/subscriptions')
      .post(subscription, (errr,result) => {
        if (result)
          console.log(result);  
        //process subscription
      });
  } catch (err) {
    console.log(err)
  }
  res.redirect('/calendar');

});

When I create an event, Graph notifies multiple times in post webhook endpoint and then continues for deletes and updates also.

Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40

1 Answers1

0

This is by design, the Microsoft Graph Webhooks service can send you duplicate notifications for the same events. Your code needs to handle this scenario, some notifications carry a unique id to help you keep tracks of the deliveries

baywet
  • 4,377
  • 4
  • 20
  • 49
  • which field is unique. Do I need to save any field in my database and check that field before performing edit , delete in event ? @baywet – Shyam sundar shah Feb 07 '20 at 14:20
  • Depending on which entities you are tracking you should look at the combination resourceData/@odata.id (or id) and resourceData/@odata.etag https://learn.microsoft.com/en-us/graph/webhooks?context=graph%2Fapi%2F1.0&view=graph-rest-1.0#notification-example – baywet Feb 07 '20 at 16:24
  • I tried and it is giving same id is updated, deleted even there is not any change. If i save in database . I need to checked in each second which make database busy . – Shyam sundar shah Feb 07 '20 at 18:13
  • 1
    Can't you use another kind of storage for this in addition to your database? Like in memory distributed? (Eg redis ) it'd be better suited – baywet Feb 08 '20 at 20:45