Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: Adds instructions for receiving "old" data on subscriptions

Paul Copplestoned1fc5647d68b
diff --git a/web/docs/library/subscribe.mdx b/web/docs/library/subscribe.mdx
index 28f61647b77..36c033fdca6 100644
--- a/web/docs/library/subscribe.mdx
+++ b/web/docs/library/subscribe.mdx
@@ -8,7 +8,16 @@ 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'
+import {
+ subscribeAllJs,
+ subscribeTableJs,
+ subscribeInsertsJs,
+ subscribeUpdatesJs,
+ subscribeDeletesJs,
+ subscribeMultipleJs,
+ subscribeUnsubscribeJs,
+ subscribeRemoveJs,
+} from './examples/subscribe'
Subscribe to realtime changes in your own database.
@@ -24,6 +33,10 @@ We will be using these tables as reference for our examples.
jsHighlight="{8}"
/>
+Note, 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
@@ -32,6 +45,10 @@ We will be using these tables as reference for our examples.
jsHighlight="{8}"
/>
+Note, 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
@@ -48,6 +65,10 @@ We will be using these tables as reference for our examples.
jsHighlight="{9-11}"
/>
+Note, 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
@@ -56,6 +77,9 @@ We will be using these tables as reference for our examples.
jsHighlight="{9-11}"
/>
+You will not be able to delete data from your database unless you set your set your table to use FILL `REPLICA IDENTITY`, like this:
+`ALTER TABLE your_table REPLICA IDENTITY FULL;`
+
<!-- ### Listening to multiple events
<CustomCodeBlock
@@ -84,7 +108,7 @@ We will be using these tables as reference for our examples.
### `from()`
-<FromFunction verb='subscribe' />
+<FromFunction verb="subscribe" />
### `on()`
@@ -96,12 +120,14 @@ const mySubscription = supabase
```
#### `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()`
@@ -113,15 +139,15 @@ const mySubscription = supabase
.subscribe()
```
-Subscribes to the database table specified in `from()` and starts listening to it. An object of type *subscription* will be returned.
+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()`
@@ -130,11 +156,11 @@ If the subscription already exists, but has been unsubscribed from with the func
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.
-----
+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()`
@@ -144,12 +170,13 @@ supabase
.removeSubscription(mySubscription)
```
-Disconnects and destroys a subscription. This is the preferred method for cleaning up subscriptions which you no longer require.
+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()`
@@ -158,8 +185,8 @@ A subscription that was previously created. If you don't have access to the subs
supabase
.getSubscriptions()
```
-Returns an array of all your subscriptions (including disconnected subscriptions).
+Returns an array of all your subscriptions (including disconnected subscriptions).
<Collapsable title="Toggle Example Response">
@@ -191,12 +218,9 @@ Returns an array of all your subscriptions (including disconnected subscriptions
]
```
-
</Collapsable>
-
-----
-
+---
## Change Events
@@ -208,55 +232,54 @@ Upon inserting or creating a new row, the following will be returned by our list
```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"]
- }
+ "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"]
- }
+ "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
- },
+ "schema": "public",
+ "table": "countries",
+ "commit_timestamp": "2020-01-01T00:00:00Z",
+ "eventType": "DELETE",
+ "old": {
+ "id": 76
}
+}
```