mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Closes DOCS-1614 ## Problem This PR is to apply the template to top-five low-hanging fruit pages -- ones that nearly match the template already and do not need to break out new topics. This PR also tests the template to see that it is working as expected. ## Solution Apply the How-to Template to 5 how-to pages. ## Preview links | Site | Live | Preview | Search for | | --- | --- | --- | --- | | Docs | [/docs/guides/storage/analytics/creating-analytics-buckets](https://supabase.com/docs/guides/storage/analytics/creating-analytics-buckets) | [/docs/guides/storage/analytics/creating-analytics-buckets](https://docs-git-docs-howto-rework-easy-pages-supabase.vercel.app/docs/guides/storage/analytics/creating-analytics-buckets) | Create an analytics bucket | | Docs | [/docs/guides/functions/debugging-tools](https://supabase.com/docs/guides/functions/debugging-tools) | [/docs/guides/functions/debugging-tools](https://docs-git-docs-howto-rework-easy-pages-supabase.vercel.app/docs/guides/functions/debugging-tools) | Debug Edge Functions locally | | Docs | [/docs/guides/queues/consuming-messages-with-edge-functions](https://supabase.com/docs/guides/queues/consuming-messages-with-edge-functions) | [/docs/guides/queues/consuming-messages-with-edge-functions](https://docs-git-docs-howto-rework-easy-pages-supabase.vercel.app/docs/guides/queues/consuming-messages-with-edge-functions) | Consume queue messages | | Docs | [/docs/guides/database/postgres/setup-replication-external](https://supabase.com/docs/guides/database/postgres/setup-replication-external) | [/docs/guides/database/postgres/setup-replication-external](https://docs-git-docs-howto-rework-easy-pages-supabase.vercel.app/docs/guides/database/postgres/setup-replication-external) | Replicate to an external Postgres | | Docs | [/docs/guides/security/rotate-leaked-api-keys](https://supabase.com/docs/guides/security/rotate-leaked-api-keys) | [/docs/guides/security/rotate-leaked-api-keys](https://docs-git-docs-howto-rework-easy-pages-supabase.vercel.app/docs/guides/security/rotate-leaked-api-keys) | Rotate a leaked API key | ## Review instructions 1. Open the live and preview links for each page side by side. 2. Check that each page opens with a value statement and ends with Next steps. 3. Check that the code blocks and the Dashboard steps match the live page. 4. On the debugging page, check that the steps run in order across the two phases. 5. On the replication page, check that the code blocks and notes render under the right steps and the numbering runs 1 to 5. 6. On the rotate page, check that the "Retire the compromised key" link in Prerequisites still jumps to its heading. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
126 lines
4.1 KiB
Plaintext
126 lines
4.1 KiB
Plaintext
---
|
|
title: Consume queue messages
|
|
subtitle: 'Learn how to consume Supabase Queue messages server-side with a Supabase Edge Function'
|
|
description: 'Read Supabase Queue messages in an Edge Function, process each one, and delete it when done, so failed messages stay on the queue and run again next time.'
|
|
---
|
|
|
|
Read and process queue messages server-side with a Supabase Edge Function. For more details on the API, see the [Queues API reference](/docs/guides/queues/api).
|
|
|
|
## Overview
|
|
|
|
Supabase Queues is a pull-based Message Queue consisting of three main components: Queues, Messages, and Queue Types.
|
|
|
|
Every time the Edge Function below is run it:
|
|
|
|
1. Read 5 messages off the queue
|
|
2. Call the `processMessage` function
|
|
3. At the end of `processMessage`, the message is deleted from the queue
|
|
4. If `processMessage` throws an error, the error is logged. In this case, the message is still in the queue, so the next time this Edge Function runs it reads the message again.
|
|
|
|
## Prerequisites
|
|
|
|
You need to be familiar with the [Queues Quickstart](/docs/guides/queues/quickstart).
|
|
|
|
## Consuming messages in an Edge Function
|
|
|
|
This is a [Supabase Edge Function](/docs/guides/functions) that reads 5 messages off the queue, processes each of them, and deletes each message when it is done.
|
|
|
|
```tsx
|
|
import 'jsr:@supabase/functions-js/edge-runtime.d.ts'
|
|
|
|
import { createClient } from 'npm:@supabase/supabase-js@2'
|
|
|
|
const supabaseUrl = 'supabaseURL'
|
|
const supabaseKey = 'supabaseKey'
|
|
|
|
const supabase = createClient(supabaseUrl, supabaseKey)
|
|
const queueName = 'your_queue_name'
|
|
|
|
// Type definition for queue messages
|
|
interface QueueMessage {
|
|
msg_id: bigint
|
|
read_ct: number
|
|
vt: string
|
|
enqueued_at: string
|
|
message: any
|
|
}
|
|
|
|
async function processMessage(message: QueueMessage) {
|
|
//
|
|
// Do whatever logic you need to with the message content
|
|
//
|
|
// Delete the message from the queue
|
|
const { error: deleteError } = await supabase.schema('pgmq_public').rpc('delete', {
|
|
queue_name: queueName,
|
|
msg_id: message.msg_id,
|
|
})
|
|
|
|
if (deleteError) {
|
|
console.error(`Failed to delete message ${message.msg_id}:`, deleteError)
|
|
} else {
|
|
console.log(`Message ${message.msg_id} deleted from queue`)
|
|
}
|
|
}
|
|
|
|
Deno.serve(async (req) => {
|
|
const { data: messages, error } = await supabase.schema('pgmq_public').rpc('read', {
|
|
queue_name: queueName,
|
|
sleep_seconds: 0, // Don't wait if queue is empty
|
|
n: 5, // Read 5 messages off the queue
|
|
})
|
|
|
|
if (error) {
|
|
console.error(`Error reading from ${queueName} queue:`, error)
|
|
return new Response(JSON.stringify({ error: error.message }), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
if (!messages || messages.length === 0) {
|
|
console.log('No messages in workflow_messages queue')
|
|
return new Response(JSON.stringify({ message: 'No messages in queue' }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
console.log(`Found ${messages.length} messages to process`)
|
|
|
|
// Process each message that was read off the queue
|
|
for (const message of messages) {
|
|
try {
|
|
await processMessage(message as QueueMessage)
|
|
} catch (error) {
|
|
console.error(`Error processing message ${message.msg_id}:`, error)
|
|
}
|
|
}
|
|
|
|
// Return immediately while background processing continues
|
|
return new Response(
|
|
JSON.stringify({
|
|
message: `Processing ${messages.length} messages in background`,
|
|
count: messages.length,
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}
|
|
)
|
|
})
|
|
```
|
|
|
|
## Next steps
|
|
|
|
To run this Edge Function, use one of these:
|
|
|
|
- [Supabase Cron](/docs/guides/cron): set up Cron so that every N number of minutes or seconds, the Edge Function runs and processes a number of messages off the queue.
|
|
- [`supabase.functions.invoke`](/docs/guides/functions/quickstart-dashboard#usage): invoke the Edge Function on command at any given time.
|
|
|
|
## Related resources
|
|
|
|
To read more about Queues, see these pages:
|
|
|
|
- [Queues API reference](/docs/guides/queues/api): look up every function for sending, reading, and deleting messages.
|
|
- [Queues Quickstart](/docs/guides/queues/quickstart): create a queue and send your first message.
|