Subscribing
Subscribe to realtime changes in your own database.
We will be using these tables as reference for our examples.
Toggle Example Tables
We will be fictional "world" database in all of our examples.
id | name | iso2 | population_range_millions | main_exports |
---|---|---|---|---|
76 | Brazil | BR | ( 205, 215 ) | [ beans, minerals ] |
156 | China | CN | ( 1380, 1390 ) | [ computers, phones ] |
250 | France | FR | ( 60, 70 ) | [ cars, machines ] |
554 | New Zealand | NZ | ( 4, 6 ) | [ food, machines ] |
556 | Nigeria | NG | ( 185, 195 ) | [ oil, beans ] |
840 | United States | US | ( 320, 330 ) | [ oil, cars, food ] |
name | country_id |
---|---|
Rio de Janeiro | 76 |
Beijing | 156 |
Paris | 250 |
Auckland | 554 |
Lagos | 556 |
Los Angeles | 840 |
San Francisco | 840 |
Listen to all database changes
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
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
Listening only to UPDATE
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
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;
Unsubscribing
Removing a subscription
Reference
from()
tableName: string
Name of the database table to be listened to. Using the wildcard "*"
will lead to all tables to be listened to.
on()
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()
Subscribes to the database table specified in from()
and starts listening to it. An object of type subscription will be returned.
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()
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()
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()
Returns an array of all your subscriptions (including disconnected subscriptions).
Toggle Example Response
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:
Existing record updated
Upon updating a row, the following will be returned by our listener: `
Existing record deleted
Upon deleting a row, the following will be returned by our listener: