This new release of the Nylas Node SDK brings a few couple of new features, most notably the release of local webhook development support built directly into the SDK. When implementing this feature in your app, the SDK will create a tunnel connection to a websocket server and registers it as a webhook callback to your Nylas account. See below for a usage example.
Release Notes
Added
Usage
Webhook Tunnel
During the setup process you can pass in methods to override the websocket client's callback methods. The most important method is the onMessage
method which returns a parsed delta event from the webhook server.
const Nylas = require('nylas');
const { openWebhookTunnel } = require('nylas/lib/services/tunnel');
const { WebhookTriggers } = require('nylas/lib/models/webhook');
// Initialize an instance of the Nylas SDK using the client credentials
Nylas.config({
clientId: "CLIENT_ID",
clientSecret: "CLIENT_SECRET",
});
// Define the callback for the onMessage event
const onMessageListener = (delta) => {
if(delta.type === WebhookTriggers.MessageUpdated) {
console.log("Got message from webhook. Data: " + delta.objectData);
}
}
// Pass your config in to create, register, and open the webhook tunnel for testing
openWebhookTunnel({
onMessage: onMessageListener,
triggers: [WebhookTriggers.MessageUpdated]
}).then(r => console.log("Webhook connected and active. " + r))