Learn how to set up real-time listeners in Supabase with a step-by-step guide to create a project, configure your table, and integrate real-time updates in your app.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Create a Supabase Project
Step 2: Set up the Database Table
id
, name
, description
).id
field with an auto-incrementing integer.
Step 3: Configure Real-time Capabilities
Step 4: Install the Supabase Client Library
To interact with Supabase in your application, you need to install the Supabase client library.
If you’re using JavaScript, install it using npm:
npm install @supabase/supabase-js
Step 5: Set up Real-time Listener in Your App
Import the Supabase client library and initialize a client with your Supabase URL and public anon
key:
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'https://YOUR_SUPABASE_URL';
const supabaseAnonKey = 'YOUR_PUBLIC_ANON\_KEY';
const supabase = createClient(supabaseUrl, supabaseAnonKey);
Set up a real-time listener for a table:
const subscription = supabase
.from('YOUR_TABLE_NAME')
.on('\*', payload => {
console.log('Change received!', payload);
})
.subscribe();
Step 6: Handle Incoming Real-time Changes
Within the .on('*', callback)
function, you can handle different types of changes (INSERT
, UPDATE
, DELETE
) as needed:
.on('INSERT', payload => {
console.log('New row!', payload.new);
})
.on('UPDATE', payload => {
console.log('Updated row!', payload.new);
})
.on('DELETE', payload => {
console.log('Deleted row!', payload.old);
})
Step 7: Unsubscribe When Necessary
If needed, you can unsubscribe from the real-time updates:
supabase.removeSubscription(subscription);
This is usually done when a component unmounts or your application shuts down to prevent memory leaks.
Conclusion
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.