If you're looking for a way to run background jobs, Supabase and n8n are two great options. Supabase is a open source Firebase alternative, and n8n is a open source Node-based workflow automation tool. Together they are a powerfully combination for background tasks.
In this post, I'll show you how I to used these two tools for my projects einfachcrypto.de and illostration.com.
If you have not worked with Supabase or n8n, here are some steps on how you can set these up.
Project Settings
-> API
. You can read more hereMy passion project, einfachcrypto.de, lists the top 250 cryptocurrencies. It is a static site built with GatsbyJS. The list of cryptocurrencies is saved in a coins
table in Supabase, and the gatsby-source-supabase plugin pulls all the data into the frontend. CoinGecko is the main datasource. As the data, especially the price, changes quite often, I needed a way to update some of the data regularly. I am using n8n for a cron-triggered job, which fetches the latest information from CoinGecko and updates the coin entry in Supabase. This is how my workflow looks like.
Explanation of the steps
I am using replicate.com to run the stable diffusion AI model for illostration.com. It is an AI infrastructure service that runs AI image generation (they call them predictions) for me. The API is built so that you first send a post request which triggers the prediction. You then have to poll every second and check the status of the prediction to see if it is finished. The response also includes the generated image. In Supabase, each prediction is saved in a predictions table, so I can track the status and retrieve the image.
This polling usually happens on the client side, but I have a case where I create multiple predictions in bulk. I am using a background job to update the status of the prediction and to retrieve the generated images. As I have production and staging Supabase environments, I am doing the same steps for both.
Explanation of the steps
pending
.prediction
tableI used the nextjs-subscription-payment boilerplate for illostration.com. This boilerplate already includes two tables, subscriptions
and users
. For my credit system, I decided to keep it simple and just add a column for credits
on the users
table. Now I only have to check for active subscriptions and reset the credits at the end of the month. Each subscription includes the user_id and the amount of credits included.
Explanation of the steps
user_id
and reset credits to the subscribed amount.PS: This post was initially published on my blog