DEV Community

Cover image for Receive event notifications from your nodejs backend
Shash
Shash

Posted on

Receive event notifications from your nodejs backend

While building products, there are many times where you need to get notified of a json payload, env vars in production, webhook data, etc.

Unfortunately, most notification tools are designed for simple user-to-user tasks.

In comes Operational, a Event tracker tool. With this tool, pass json, structured json, image urls and more in your events and get notified in the webapp or on your mobile.

Image description

Let's get started.

Sending your first event

  • Signup to Operational.co and get your API token
  • Install the Operational SDK
import Operational from "@operational.co/sdk"
Enter fullscreen mode Exit fullscreen mode

Then paste this code in your project. Replace the TOKEN with your API key.

const ops = new Operational("TOKEN");

await ops.events.ingest({
  name : "User trial ended",
  avatar : "πŸ€”"
});
Enter fullscreen mode Exit fullscreen mode

That's it!

Taking it further

Now let's try sending a large javascript object to operational. This could be useful for getting notified of Stripe webhooks, 3rd party API webhooks, etc.

Image description

Run this code:

const webhookData = {
}
await ops.events.ingest({
  name : "User signed up",
  avatar : "πŸ€”",
  type : "json",
  content : {
    user_name : "Shash",
    email : "shash@operational",
    id : 13760
  }
});
Enter fullscreen mode Exit fullscreen mode

Contextual events

Sometimes, it is helpful to chain events together.

Image description

This is super helpful for understanding complex workflows, cronjobs and other scenarios. This is what Operational calls contextual events.

Here’s how you can setup contextual events:

/*
 * Set a unique Id to chain events together.
 * We will use this id for related events later
 */
const contextId = "user-12312-signup";

// First event
await ops.events.ingest({
  name : "User signup",
  avatar : "πŸ€”",
  contextStart : true,
  contextId : contextId,
  actions : [
    {
      key : "Ban",
      url: "https://xyz.com/webhook/ban",
    }
  ]
});

// second event some time later
await ops.events.ingest({
  name : "User activated",
  avatar : "😁",
  contextId : contextId,
});

// third event
await ops.events.ingest({
  name : "User created their project",
  avatar : "πŸ‘",
  contextId : contextId,
  content : "Project Operational created for user Shash"
});
Enter fullscreen mode Exit fullscreen mode

As long as the contextId is same, all these events will show up in the β€˜chain’.

Conclusion

I hope this guide has been helpful for you. Let me know in the comments how you're using Operational to track events in your own backend!

Top comments (0)