Realtime not working in Node SSR

Hello! I'm struggling a lot to implement a feature in my Express app that needs Realtime capabilities, can someone help me out? 😓 My idea is to use realtime channels to listen and forward events from an "ambulance" table for users that connect to a "watch" route. To accomplish that, I'm using the @supabase/ssr package. I've verified that Realtime is active in my "ambulance" table and the user I'm using to test is allowed by the RLS rules, as I've tested by fetching ambulances while connected as they. The problem is that even though my connection succeds, as my subscribe callback logs SUBSCRIBED, the callback passed to the on method - responsible for notifying changes - is not being called when I make changes to "ambulance" in the DB. Here's the code in my ambulance controller that connects to realtime channels:
typescript
export function watch(req: WatchRequest, res: Response) {
const { user, supabase } = req;

if (!supabase)
throw new Error("Supabase client is required to watch ambulances");
if (!user) throw new Error("You must be authenticated to watch ambulances");

const channel = supabase
.channel(`ambulance-${user.id}`)
.on(
"postgres_changes",
{ event: "*", schema: "public", table: "ambulance" },
(payload) => {
switch (payload.eventType) {
case "INSERT":
res.json(payload.new);
break;
case "UPDATE":
res.json(payload.new);
break;
case "DELETE":
res.json(payload.old);
}
}
)
.subscribe((status, error) => {
if (error) {
throw error;
} else {
console.log(status);
}
});
}
typescript
export function watch(req: WatchRequest, res: Response) {
const { user, supabase } = req;

if (!supabase)
throw new Error("Supabase client is required to watch ambulances");
if (!user) throw new Error("You must be authenticated to watch ambulances");

const channel = supabase
.channel(`ambulance-${user.id}`)
.on(
"postgres_changes",
{ event: "*", schema: "public", table: "ambulance" },
(payload) => {
switch (payload.eventType) {
case "INSERT":
res.json(payload.new);
break;
case "UPDATE":
res.json(payload.new);
break;
case "DELETE":
res.json(payload.old);
}
}
)
.subscribe((status, error) => {
if (error) {
throw error;
} else {
console.log(status);
}
});
}
Also, here's a Minimal Reproduction Project, so the project structure can be better understood: https://github.com/ambulab-fenix/supabase-ssr-realtime Thanks in advance to anyone that can give me a hand! 🙏
GitHub
GitHub - ambulab-fenix/supabase-ssr-realtime
Contribute to ambulab-fenix/supabase-ssr-realtime development by creating an account on GitHub.
1 Reply
garyaustin
garyaustin7h ago
If you turn off RLS on the table does it work?

Did you find this page helpful?