Skip to content

Commit 8047e31

Browse files
committed
docs(advanced): realtime example
1 parent cb30403 commit 8047e31

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

docs/content/3.usage/1.composables.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ Here is an example of a fetch using the `select` method with Nuxt 3 [useAsyncDat
6262

6363
```vue
6464
<script setup lang="ts">
65-
const user = useSupabaseUser()
6665
const client = useSupabaseClient()
6766
6867
const { data: restaurant } } = await useAsyncData('restaurant', async () => {

docs/content/4.advanced.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,39 @@ title: Advanced
33
description: 'Real-life advanced usages of the supabase module.'
44
---
55

6+
## Realtime
7+
8+
Based on [Supabase Realtime](https://github.com/supabase/realtime), listen to changes in your PostgreSQL Database and broadcasts them over WebSockets.
9+
10+
To enable it, make sure you have turned on the [Real Time API](https://supabase.com/docs/guides/api#realtime-api-1) for your table.
11+
12+
Then, listen to changes directly in your vue page / component:
13+
14+
```vue
15+
<script setup lang="ts">
16+
import type { RealtimeSubscription } from '@supabase/supabase-js'
17+
18+
const client = useSupabaseClient()
19+
let subscription: RealtimeSubscription
20+
21+
// Fetch collaborators and get the refresh method provided by useAsyncData
22+
const { data: collaborators, refresh: refreshCollaborators } = await useAsyncData('collaborators', async () => {
23+
const { data } = await client.from('collaborators').select('name')
24+
return data
25+
})
26+
27+
// Once page is mounted, listen to changes on the `collaborators` table and refresh collaborators when receiving event
28+
subscription = client.from('collaborators').on('*', () => {
29+
refreshCollaborators()
30+
}).subscribe()
31+
32+
// Don't forget to unsubscribe when user left the page
33+
onUnmounted(() => {
34+
client.removeSubscription(subscription)
35+
})
36+
</script>
37+
```
38+
639
## Auth middleware
740

841
You can protect your authenticated routes by creating a custom composable in your project, here is an example:

0 commit comments

Comments
 (0)