mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
294 lines
6.2 KiB
Plaintext
294 lines
6.2 KiB
Plaintext
---
|
|
id: subscribe
|
|
title: 'Subscribing'
|
|
description: 'Subscribe to realtime changes in your database with Supabase'
|
|
---
|
|
|
|
import DummyData from '../common/_DummyData.mdx'
|
|
import FromFunction from '../common/_FromFunction.mdx'
|
|
import Collapsable from '../../src/components/Collapsable'
|
|
import CustomCodeBlock from '../../src/components/CustomCodeBlock'
|
|
import {
|
|
subscribeAllJs,
|
|
subscribeTableJs,
|
|
subscribeInsertsJs,
|
|
subscribeUpdatesJs,
|
|
subscribeDeletesJs,
|
|
subscribeMultipleJs,
|
|
subscribeUnsubscribeJs,
|
|
subscribeRemoveJs,
|
|
} from './examples/subscribe'
|
|
|
|
Subscribe to realtime changes in your own database.
|
|
|
|
We will be using these tables as reference for our examples.
|
|
|
|
<DummyData />
|
|
|
|
### Listen to all database changes
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes to all database changes in realtime."
|
|
js={subscribeAllJs}
|
|
jsHighlight="{8}"
|
|
/>
|
|
|
|
<br />
|
|
|
|
If you want to receive the "previous" data for updates and deletes,
|
|
you will need to set `REPLICA IDENTITY` to `FULL`, like this:
|
|
`ALTER TABLE your_table REPLICA IDENTITY FULL;`
|
|
|
|
### Listening to a specific table
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes to a specific table for changes in realtime."
|
|
js={subscribeTableJs}
|
|
jsHighlight="{8}"
|
|
/>
|
|
|
|
<br />
|
|
|
|
If you want to receive the "previous" data for updates and deletes,
|
|
you will need to set `REPLICA IDENTITY` to `FULL`, like this:
|
|
`ALTER TABLE your_table REPLICA IDENTITY FULL;`
|
|
|
|
### Listening only to INSERT
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes only to the INSERT event."
|
|
js={subscribeInsertsJs}
|
|
jsHighlight="{9-11}"
|
|
/>
|
|
|
|
### Listening only to UPDATE
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes only to the UPDATE event."
|
|
js={subscribeUpdatesJs}
|
|
jsHighlight="{9-11}"
|
|
/>
|
|
|
|
<br />
|
|
|
|
If you want to receive the "previous" data for updates,
|
|
you will need to set `REPLICA IDENTITY` to `FULL`, like this:
|
|
`ALTER TABLE your_table REPLICA IDENTITY FULL;`
|
|
|
|
### Listening only to DELETE
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes only to the DELETE event."
|
|
js={subscribeDeletesJs}
|
|
jsHighlight="{9-11}"
|
|
/>
|
|
|
|
<br />
|
|
|
|
You will not be able to delete data from your database unless you set your set your table to use FULL `REPLICA IDENTITY`, like this:
|
|
`ALTER TABLE your_table REPLICA IDENTITY FULL;`
|
|
|
|
<!-- ### Listening to multiple events
|
|
|
|
<CustomCodeBlock
|
|
header="Subscribes to multiple but not all events."
|
|
js={subscribeMultipleJs}
|
|
jsHighlight="{9-10}"
|
|
/> -->
|
|
|
|
### Unsubscribing
|
|
|
|
<CustomCodeBlock
|
|
header="Unsubscribes from a specific subscription."
|
|
js={subscribeUnsubscribeJs}
|
|
jsHighlight="{15}"
|
|
/>
|
|
|
|
### Removing a subscription
|
|
|
|
<CustomCodeBlock
|
|
header="Disconnects from a subscription fully."
|
|
js={subscribeRemoveJs}
|
|
jsHighlight="{15}"
|
|
/>
|
|
|
|
## Reference
|
|
|
|
### `from()`
|
|
|
|
<FromFunction verb="subscribe" />
|
|
|
|
### `on()`
|
|
|
|
<!-- prettier-ignore -->
|
|
```js {3}
|
|
const mySubscription = supabase
|
|
.from('tableName')
|
|
.on('eventType', callbackFunction)
|
|
```
|
|
|
|
#### `eventType: string { "INSERT" | "UPDATE" | "DELETE" | "*" }`
|
|
|
|
The database event which you would like to receive updates for, or you can use the special wildcard `*` to listen to all changes.
|
|
|
|
#### `callbackFunction: function`
|
|
|
|
A callback that will handle the payload that is sent whenever your database changes. See all Change Events below to understand all the possible payloads.
|
|
|
|
---
|
|
|
|
### `subscribe()`
|
|
|
|
<!-- prettier-ignore -->
|
|
```js {4}
|
|
const mySubscription = supabase
|
|
.from(tableName)
|
|
.on(eventType, callbackFunction)
|
|
.subscribe()
|
|
```
|
|
|
|
Subscribes to the database table specified in `from()` and starts listening to it. An object of type _subscription_ will be returned.
|
|
|
|
```js {1}
|
|
mySubscription.subscribe()
|
|
```
|
|
|
|
If the subscription already exists, but has been unsubscribed from with the function `unsubscribe()`, it can be subscribed to again through the above method.
|
|
|
|
---
|
|
|
|
### `unsubscribe()`
|
|
|
|
<!-- prettier-ignore -->
|
|
```js {2}
|
|
mySubscription
|
|
.unsubscribe()
|
|
```
|
|
|
|
Stop listening to changes. At any point you can call `subscribe` on the subscription object to restart the listeners.
|
|
If you don't need to connect again in the future, use `removeSubscription()` instead.
|
|
|
|
---
|
|
|
|
### `removeSubscription()`
|
|
|
|
<!-- prettier-ignore -->
|
|
```js {2}
|
|
supabase
|
|
.removeSubscription(mySubscription)
|
|
```
|
|
|
|
Disconnects and destroys a subscription. This is the preferred method for cleaning up subscriptions which you no longer require.
|
|
|
|
#### `mySubscription: subscription`
|
|
|
|
A subscription that was previously created. If you don't have access to the subscription, you can always call `getSubscriptions()`.
|
|
|
|
---
|
|
|
|
### `getSubscriptions()`
|
|
|
|
<!-- prettier-ignore -->
|
|
```js {2}
|
|
supabase
|
|
.getSubscriptions()
|
|
```
|
|
|
|
Returns an array of all your subscriptions (including disconnected subscriptions).
|
|
|
|
<Collapsable title="Toggle Example Response">
|
|
|
|
<!-- prettier-ignore -->
|
|
```js
|
|
[
|
|
{
|
|
uuid: 'xxxx-xxxx-xxxx-xxxx',
|
|
state: 'CONNECTED',
|
|
tableName: 'users',
|
|
eventListeners: {
|
|
'*': null,
|
|
'INSERT': null,
|
|
'UPDATE': null,
|
|
'DELETE': null,
|
|
}
|
|
},
|
|
{
|
|
uuid: 'yyyy-yyyy-yyyy-yyyy',
|
|
state: 'DISCONNECTED',
|
|
tableName: 'messages',
|
|
eventListeners: {
|
|
'*': null,
|
|
'INSERT': null,
|
|
'UPDATE': null,
|
|
'DELETE': null,
|
|
}
|
|
}
|
|
]
|
|
```
|
|
|
|
</Collapsable>
|
|
|
|
---
|
|
|
|
## Change Events
|
|
|
|
@TODO what happens on bulk insert/update/delete?
|
|
|
|
### New record created
|
|
|
|
Upon inserting or creating a new row, the following will be returned by our listener:
|
|
|
|
```json
|
|
{
|
|
"schema": "public",
|
|
"table": "countries",
|
|
"commit_timestamp": "2020-01-01T00:00:00Z",
|
|
"eventType": "INSERT",
|
|
"new": {
|
|
"id": 533,
|
|
"name": "Columbia",
|
|
"iso2": "CO",
|
|
"population_range_millions": [40, 50],
|
|
"main_exports": ["oil", "minerals"]
|
|
}
|
|
}
|
|
```
|
|
|
|
### Existing record updated
|
|
|
|
Upon updating a row, the following will be returned by our listener:
|
|
`
|
|
|
|
```json
|
|
{
|
|
"schema": "public",
|
|
"table": "countries",
|
|
"commit_timestamp": "2020-01-01T00:00:00Z",
|
|
"eventType": "UPDATE",
|
|
"old": {},
|
|
"new": {
|
|
"id": 76,
|
|
"name": "Brazil",
|
|
"iso2": "BR",
|
|
"population_range_millions": [205, 215],
|
|
"main_exports": ["beans", "minerals"]
|
|
}
|
|
}
|
|
```
|
|
|
|
### Existing record deleted
|
|
|
|
Upon deleting a row, the following will be returned by our listener:
|
|
|
|
```json
|
|
{
|
|
"schema": "public",
|
|
"table": "countries",
|
|
"commit_timestamp": "2020-01-01T00:00:00Z",
|
|
"eventType": "DELETE",
|
|
"old": {
|
|
"id": 76
|
|
}
|
|
}
|
|
```
|