/supabase-tutorials

How to set up real-time listeners in Supabase?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to set up real-time listeners in Supabase?

 

Step 1: Create a Supabase Project

 

  • Go to the Supabase website and log in to your account.
  • Click on the “New project” button.
  • Enter a name for your project and select your preferred database password.
  • Choose the region closest to you for optimal performance and click “Create new project”.

 

Step 2: Set up the Database Table

 

  • Once your project is created, go to the “Table Editor” in the Supabase dashboard.
  • Click on “New Table” to create a new table where you’ll be listening for changes.
  • Define your table columns based on your requirements (e.g., id, name, description).
  • Make sure to set a primary key, often an id field with an auto-incrementing integer.
  • Save your changes.

 

Step 3: Configure Real-time Capabilities

 

  • By default, Supabase enables real-time functions for all tables. However, to ensure it’s active:
  • Navigate to “Database” > “Replication” in the Supabase dashboard.
  • Ensure that the tables you intend to listen to have enabled "Replicate this data in real-time".

 

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

 

  • With the above steps, you’ve set up real-time listeners using Supabase in your application. Real-time listeners allow your app to react to changes in your database immediately, providing a seamless user experience.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022