Files
supabase/web/docs/client/subscribe.mdx
T

331 lines
5.0 KiB
Plaintext

---
id: subscribe
title: "on().subscribe()"
slug: subscribe
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Subscribe to realtime changes in your databse.
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('*')
.on('*', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
## Parameters
<ul className="method-list-group">
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
event
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
INSERT | UPDATE | DELETE | *
</span>
</h4>
<div class="method-list-item-description">
The database event which you would like to receive updates for, or you can use the special wildcard `*` to listen to all changes.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
callback
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
object
</span>
</h4>
<div class="method-list-item-description">
A callback that will handle the payload that is sent whenever your database changes.
</div>
</li>
</ul>
## Notes
- 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;`
## Examples
### Listen to all database changes
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('*')
.on('*', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to a specific table
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries')
.on('*', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to inserts
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries')
.on('INSERT', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to updates
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries')
.on('UPDATE', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to deletes
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries')
.on('DELETE', payload => {
console.log('Change received!', payload)
})
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to multiple events
You can chain listeners if you want to listen to multiple events for each table.
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries')
.on('INSERT', handleRecordInserted)
.on('DELETE', handleRecordDeleted)
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>
### Listening to row level changes
You can listen to individual rows using the format `{table}:{col}=eq.{val}` - where `{col}` is the column name, and `{val}` is the value which you want to match.
<Tabs
defaultValue="js"
groupId="libraries"
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
<TabItem value="js">
```js
const mySubscription = supabase
.from('countries:id=eq.200')
.on('UPDATE', handleRecordUpdated)
.subscribe()
```
</TabItem>
<TabItem value="py">
```py
# Not yet implemented
```
</TabItem>
</Tabs>