Upstash QStash
Learn how to set up background jobs, cron jobs and queues with Upstash in your supastarter application.
Upstash QStash is a reliable message queue and job scheduler that's perfect for serverless environments. It allows you to create background jobs, schedule tasks, and handle webhook processing with ease.
Prerequisites
Before getting started, you'll need:
- An Upstash account
- A QStash service created in your Upstash dashboard
- Your QStash credentials (URL and token)
1. Install Dependencies
First, install the QStash SDK:
2. Environment Variables
Add the following environment variables to your .env.local
file:
You can find these values in your Upstash dashboard under the QStash service.
3. Create the QStash Client
Create a utility file to initialize the QStash client:
4. Create a task router
Create a new router to handle your tasks:
5. Trigger a task
To trigger your task, you can use the qstashClient
to send a request to the task router:
6. Usage Examples
Cron jobs
To schedule a task to run at a specific time, you can use the qstashClient
to create a cron job:
Queues
To create a queue, you can use the qstashClient
to create a queue and create a sequence of tasks to be executed.