Enabling PostgreSQL Realtime Monitoring with Supabase and Bun Backend: Need Help Troubleshooting

171 views Asked by At

I'm developing an application with a Supabase backend, and I'm currently attempting to deploy a separate, very simple backend using Bun with TypeScript and the Supabase-js npm library. The goal of this separate backend is to monitor database changes in the PostgreSQL real-time changes database. Whenever a new insertion or update occurs in a specific table within the PostgreSQL database, I aim to update a locally maintained array representing that table. Additionally, I want to check if any "start_time" values in the table have passed the current time, triggering the execution of other functions.

The issue I'm encountering is that I'm not receiving any changes through the Supabase real-time subscription listener.

Here's some additional context:

  • I initialize the Supabase "client" using the createClient function and provide the SERVICE_ROLE_KEY because I intend to use this service independently from the main application.
  • I'm testing this backend service in a WSL2 Windows Terminal, and I suspect there might be connectivity issues related to WebSocket connections and port settings.
  • Yes, i enabled realtime for the db table "training"

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


// Supabase-Client-Initialisierung
const supabase = createClient('https://dsafsdfasdf.supabase.co', 'foobarfoobarSUPABASE-SERVICE-ROLE-KEY', {
  auth: {
    persistSession: false,
  }
});


async function checkSession(){
  // Check if logged in
  try{
    //supabase.auth.setSession();
    const checkSession = await supabase.auth.getSession();
      if (checkSession != null){
        console.log('Get session was sucessfull. (probalby data.session = null)')
        if ( checkSession.data.session != null ){
          console.log('We got a valid session! Question is if the sessions stays...')
          console.log(checkSession.data.session)
        } else {
          **console.log('We got a null session ') //This case happens.** 
        }

        }
      else {
        console.debug('Session NULL returned! not good!')
      }
  } catch {
      throw {"error": "not logged in"}
  }
}

await checkSession();


const calculated_trainings: Training[] = [];

// fetch the initial trainings from the db
async function fetchInitialTrainings() {
// works as intendet... supabase.from ... works and retruns trainings
}

await fetchInitialTrainings();

**//Mostly important part:**
const channelA = supabase
.channel('schema-db-changes')
.on(
  'postgres_changes',
  {
    event: "*", 
    schema: 'public',
    table: 'training'
  },
  (payload) => {
    console.log('WE RECEIVED A PAYLOAD:')
    console.log(payload)}
)
.subscribe()

console.log('isConnected to realtime?:')
console.log(await supabase.realtime.isConnected()) **//returns false**

Do you have any idea how i get those postgres changes?

1

There are 1 answers

0
dshukertjr On

Not an answer, but you can add a callback to your .subscribe() method to see the subscription status to see if the subscription is succeeding.

const channelA = supabase
.channel('schema-db-changes')
.on(
  'postgres_changes',
  {
    event: "*", 
    schema: 'public',
    table: 'training'
  },
  (payload) => {
    console.log('WE RECEIVED A PAYLOAD:')
    console.log(payload)}
)
.subscribe((status) => {
  console.log({status})
})