September 02, 2024
Build real-time, durable workflows with Pulse and Inngest
Prisma and Inngest come together to show how you can make real-time, durable, and extensible workflows with ease.
Building web apps today isn’t just about functionality—it’s about creating magic for users.
Developing modern web apps is getting harder and harder as users have come to expect their experience to be beautiful, joyful, and instant while sitting on top of real-time, rich, and always-increasing data sets.
On a mission to make this easier for developers, we at Prisma bonded instantly with the team at Inngest.
Prisma Pulse was built to allow developers to respond to database changes in real-time from a Postgres database. Inngest was built to allow developers to create robust and durable background jobs and workflows -- the backbone of any great application.
In this article we demonstrate how you can build powerful, extensible, real-time workflows using Prisma Pulse and Inngest together.
Using Inngest with Prisma Pulse
Let’s demonstrate using a simple example — something you could implement in your own app on your lunch break: Onboarding a new user after they sign up.
Let’s say you have a database table called User
, which gets a new row whenever a user signs up. Then, we should send them a welcome email and schedule another email three days later.
To implement this onboarding flow, first, you start by streaming database changes to your application with just a few lines of code, to react when a new user is created:
Note: this example assumes you already installed the Pulse extension, and instantiated your Prisma client with it.
Next, we can trigger a workflow in Inngest easily by extending the above code to submit an event with the Inngest client:
To take action on that event, we’ll also need a function to handle it. Elsewhere in your project, you will create a function to respond to that event:
Using Inngest steps and sleeps, we can create an elegant workflow with more contact points over days and weeks that nurture your new user to activate.
Here’s a slightly extended example, where we can create a welcome sequence that spans over multiple days.
Now we have a fully customizable, extensible user onboarding email sequence! You can build on this to add other onboarding actions, or add custom logic. What about checking if the user tried a certain feature and then customizing the emails based on what they did or didn’t do? You can use your imagination and try different flows.
With Pulse’s Delivery Guarantees, even if your server goes down or suffers a slight interruption, Prisma will ensure that all events are delivered at least once — your server can simply pick up where it left off after recovering. So, no user will miss out on onboarding, and your workflows will be completely durable from start to finish.
Prisma Pulse + Inngest in practice: building an AI writer workflow
Now let’s explore another example featuring an AI workflow ✨
Let's say that we are building a cutting-edge CMS providing an AI Writer Assistant that reviews articles and provides suggestions.
Our CMS Editor UI is already linked into the below Prisma schema:
This time, instead of setting up Prisma Pulse from scratch, we can leverage a Pulse/Inngest router service that we created to help you get going in production in just a few minutes. It streams database changes using Prisma Pulse and sends the events to Inngest for you.
You can easily clone it from GitHub directly or set it up in seconds by running:
Note: if you want to give it a try, simply follow the instructions in the README for help with setting up and deploying the service yourself.
The above Pulse Inngest Router will transform Prisma Pulse events into Inngest events, triggering the below Inngest Function listening for the "db/article.update"
trigger:
Note: We injected the openai
client using Inngest’s Dependency Injection, which makes it available directly in the function body.
We can now implement our AI Writer workflow by writing our first “generate suggestions” and “save suggestions” steps:
Inngest’s step.run()
enables us to create a checkpoint in our workflow that will retry in case of errors (e.g., network issues or OpenAI errors). Failing to save the suggestions won’t retry the OpenAI step, as Inngest conserves the results of previous steps.
To add a bit of safety, we can also add some Throttling configuration, preventing any issues with OpenAI’s API Rate Limits and ensuring that our workflow will run a maximum of 10 times per minute.
With the AI part of the workflow now complete, let’s add a step to wait for our article to move to the PUBLISH
state before sending a Slack notification. To achieve this, let’s use Inngest’s “wait for event” feature that allows pausing a workflow until a given event arrives (Prisma Pulse will automatically send that event when the Article draft gets updated from the application to PUBLISH
state):
Note: A paused Inngest Workflow does not consume compute or throttling capacity.
Now, you have an AI-powered review system integrated directly with your existing CMS, triggered in real-time, fully extensible, and durable.
The possibilities are endless. Coupling the flexibility and power of Inngest with the real-time triggers of Prisma Pulse gives you the tools to build incredible flows, supporting an amazing user experience.
From simple moments like quickly delivering a 2FA code to in-depth and complex ones like creating a dynamic and personalized onboarding experience — you now have the tools to delight your users even more.
Want more ideas? What about triggering an ETL workflow (with events batching) when your database is updated? Or adding approval flows to existing products by hooking into the database with Pulse.
Join the conversation: What would you build with Pulse and Inngest?
Helpful resources
- Pulse/Inngest Router service (deployable)
- Getting started with Prisma
- Getting started with Inngest
- Prisma Discord community
- Inngest Discord community
- Demo Code for this article
Don’t miss the next post!
Sign up for the Prisma Newsletter