/supabase-tutorials

How to unsubscribe from real-time updates in Supabase?

Learn how to unsubscribe from real-time updates in Supabase with our step-by-step guide covering client initialization, subscription removal, and error handling.

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 unsubscribe from real-time updates in Supabase?

 

Step 1: Initialize Supabase Client

 

Before you can unsubscribe from real-time updates, ensure that you have the Supabase client set up in your project. Use the following example to initialize it:


import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'https://your-supabase-url.supabase.co';
const supabaseKey = 'your-supabase-key';
const supabase = createClient(supabaseUrl, supabaseKey);

Ensure you have replaced 'https://your-supabase-url.supabase.co' and 'your-supabase-key' with your actual Supabase project details.

 

Step 2: Subscribe to Real-time Updates

 

Before illustrating how to unsubscribe, let's see how you would typically subscribe to real-time updates.


const subscription = supabase
  .from('your\_table')
  .on('\*', payload => {
    console.log('Change received!', payload);
  })
  .subscribe();

Replace 'your_table' with the actual table name from which you want to receive updates.

 

Step 3: Unsubscribe from Real-time Updates

 

To unsubscribe from the real-time updates in Supabase, you'll use the removeSubscription method on the Supabase client object. Here is how you can achieve it:


const { data: unsubscription, error } = await supabase
  .removeSubscription(subscription);

if (error) {
  console.error('Error unsubscribing:', error.message);
} else {
  console.log('Successfully unsubscribed:', unsubscription);
}

In this step, subscription is the subscription object returned when you called the .subscribe() method earlier.

 

Step 4: Verify Unsubscription

 

After unsubscribing, it's beneficial to verify whether the operation was successful. This can be done by checking if the error object is null:


if (!error) {
  console.log('You have been unsubscribed from real-time updates successfully.');
} else {
  console.error('There was an error when attempting to unsubscribe:', error.message);
}

This will provide a console log confirming whether the unsubscription process succeeded or failed.

 

Additional Tips

 

  • Remember to handle any potential errors gracefully, especially in a production environment, to ensure a smooth user experience.
  • Consider setting up error logging to capture and handle subscription or unsubscription failures more effectively.

 

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