File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff 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()
6665const client = useSupabaseClient()
6766
6867const { data: restaurant } } = await useAsyncData('restaurant', async () => {
Original file line number Diff line number Diff line change @@ -3,6 +3,39 @@ title: Advanced
33description : ' 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
841You can protect your authenticated routes by creating a custom composable in your project, here is an example:
You can’t perform that action at this time.
0 commit comments