Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: Support private channels in the realtime inspector (#27362)

Ivan Vasilov02b3d50207b7
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx b/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
index 9719b931570..75507a329ee 100644
--- a/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
+++ b/apps/studio/components/interfaces/Realtime/Inspector/ChooseChannelPopover/index.tsx
@@ -1,24 +1,28 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { useTelemetryProps } from 'common'
-import Link from 'next/link'
import { useRouter } from 'next/router'
import { Dispatch, SetStateAction, useState } from 'react'
import { useForm } from 'react-hook-form'
import {
Button,
FormControl_Shadcn_,
+ FormDescription_Shadcn_,
FormField_Shadcn_,
FormItem_Shadcn_,
+ FormLabel_Shadcn_,
Form_Shadcn_,
IconChevronDown,
Input_Shadcn_,
PopoverContent_Shadcn_,
PopoverTrigger_Shadcn_,
Popover_Shadcn_,
+ Switch,
} from 'ui'
import * as z from 'zod'
+import { useFlag } from 'hooks'
import Telemetry from 'lib/telemetry'
+import { ExternalLink } from 'lucide-react'
import { RealtimeConfig } from '../useRealtimeMessages'
interface ChooseChannelPopoverProps {
@@ -26,17 +30,19 @@ interface ChooseChannelPopoverProps {
onChangeConfig: Dispatch<SetStateAction<RealtimeConfig>>
}
+const FormSchema = z.object({ channel: z.string(), isPrivate: z.boolean() })
+
export const ChooseChannelPopover = ({ config, onChangeConfig }: ChooseChannelPopoverProps) => {
const [open, setOpen] = useState(false)
const telemetryProps = useTelemetryProps()
const router = useRouter()
+ const authzEnabled = useFlag('authzRealtime')
- const FormSchema = z.object({ channel: z.string() })
const form = useForm<z.infer<typeof FormSchema>>({
mode: 'onBlur',
reValidateMode: 'onBlur',
resolver: zodResolver(FormSchema),
- defaultValues: { channel: '' },
+ defaultValues: { channel: '', isPrivate: false },
})
const onOpen = (v: boolean) => {
@@ -58,7 +64,12 @@ export const ChooseChannelPopover = ({ config, onChangeConfig }: ChooseChannelPo
telemetryProps,
router
)
- onChangeConfig({ ...config, channelName: form.getValues('channel'), enabled: true })
+ onChangeConfig({
+ ...config,
+ channelName: form.getValues('channel'),
+ isChannelPrivate: form.getValues('isPrivate'),
+ enabled: true,
+ })
}
return (
@@ -83,52 +94,104 @@ export const ChooseChannelPopover = ({ config, onChangeConfig }: ChooseChannelPo
{config.channelName.length === 0 ? (
<>
<Form_Shadcn_ {...form}>
- <form id="realtime-channel" onSubmit={form.handleSubmit(() => onSubmit())}>
+ <form
+ id="realtime-channel"
+ onSubmit={form.handleSubmit(() => onSubmit())}
+ className="flex flex-col gap-y-4"
+ >
<FormField_Shadcn_
name="channel"
control={form.control}
render={({ field }) => (
- <FormItem_Shadcn_>
- <label className="text-foreground text-xs mb-2">Name of channel</label>
- <div className="flex">
- <FormControl_Shadcn_>
- <Input_Shadcn_
- {...field}
- autoComplete="off"
- className="rounded-r-none text-xs px-2.5 py-1 h-auto"
- placeholder="Enter a channel name"
- />
- </FormControl_Shadcn_>
- <Button
- type="primary"
- className="rounded-l-none"
- disabled={form.getValues().channel.length === 0}
- onClick={() => onSubmit()}
- >
- Listen to channel
- </Button>
+ <FormItem_Shadcn_ className="flex flex-col gap-y-2">
+ <div className="flex flex-col gap-y-1">
+ <label className="text-foreground text-xs">Name of channel</label>
+ <div className="flex flex-row">
+ <FormControl_Shadcn_>
+ <Input_Shadcn_
+ {...field}
+ autoComplete="off"
+ className="rounded-r-none text-xs px-2.5 py-1 h-auto"
+ placeholder="Enter a channel name"
+ />
+ </FormControl_Shadcn_>
+
+ <Button
+ type="primary"
+ className="rounded-l-none"
+ disabled={form.getValues().channel.length === 0}
+ onClick={() => onSubmit()}
+ >
+ Listen to channel
+ </Button>
+ </div>
</div>
+ <FormDescription_Shadcn_ className="text-xs text-foreground-lighter">
+ The channel you initialize with the Supabase Realtime client. Learn more
+ in{' '}
+ <a
+ target="_blank"
+ rel="noreferrer"
+ className="underline hover:text-foreground transition"
+ href="https://supabase.com/docs/guides/realtime/concepts#channels"
+ >
+ our docs
+ </a>
+ </FormDescription_Shadcn_>
</FormItem_Shadcn_>
)}
/>
+
+ {authzEnabled ? (
+ <FormField_Shadcn_
+ key="isPrivate"
+ control={form.control}
+ name="isPrivate"
+ render={({ field }) => (
+ <FormItem_Shadcn_ className="">
+ <div className="flex flex-row items-center gap-x-2">
+ <FormControl_Shadcn_>
+ <Switch
+ checked={field.value}
+ onCheckedChange={field.onChange}
+ disabled={field.disabled}
+ />
+ </FormControl_Shadcn_>
+ <FormLabel_Shadcn_ className="text-xs">
+ Is channel private?
+ </FormLabel_Shadcn_>
+ </div>
+ <FormDescription_Shadcn_ className="text-xs text-foreground-lighter mt-2">
+ If the channel is marked as private, it will use RLS policies to filter
+ messages.
+ </FormDescription_Shadcn_>
+ </FormItem_Shadcn_>
+ )}
+ />
+ ) : null}
+
+ <Button asChild type="default" className="w-min" icon={<ExternalLink />}>
+ <a
+ target="_blank"
+ rel="noreferrer"
+ href="https://github.com/orgs/supabase/discussions/22484"
+ >
+ Documentation
+ </a>
+ </Button>
</form>
</Form_Shadcn_>
- <p className="text-xs text-foreground-lighter mt-2">
- The channel you initialize with the Supabase Realtime client. Learn more in{' '}
- <Link
- href="https://supabase.com/docs/guides/realtime/concepts#channels"
- target="_blank"
- rel="noreferrer"
- className="underline hover:text-foreground transition"
- >
- our docs
- </Link>
- </p>
</>
) : (
<div className="space-y-2">
<div className="flex items-center gap-x-2">
- <p className="text-foreground text-xs">Currently joined channel:</p>
+ <p className="text-foreground text-xs">
+ Currently joined{' '}
+ <span className={config.isChannelPrivate ? 'text-brand' : 'text-warning'}>
+ {config.isChannelPrivate ? 'private' : 'public'}
+ </span>{' '}
+ channel:
+ </p>
<p className="text-xs border border-scale-600 py-0.5 px-1 rounded-md bg-surface-200">
{config.channelName}
</p>
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/index.tsx b/apps/studio/components/interfaces/Realtime/Inspector/index.tsx
index 718ae8ec315..6a0bedafd1b 100644
--- a/apps/studio/components/interfaces/Realtime/Inspector/index.tsx
+++ b/apps/studio/components/interfaces/Realtime/Inspector/index.tsx
@@ -25,6 +25,7 @@ export const RealtimeInspector = () => {
token: '', // will be filled out by RealtimeTokensPopover
schema: 'public',
table: '*',
+ isChannelPrivate: false,
filter: undefined,
bearer: null,
enablePresence: true,
diff --git a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
index 46ceb7e3ad1..d70fb65d52a 100644
--- a/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
+++ b/apps/studio/components/interfaces/Realtime/Inspector/useRealtimeMessages.ts
@@ -1,5 +1,9 @@
-import { RealtimeChannel, SupabaseClient } from '@supabase/supabase-js'
-import { sortBy, take } from 'lodash'
+import { RealtimeChannel, RealtimeClient } from '@supabase/realtime-js-next'
+import {
+ DEFAULT_GLOBAL_OPTIONS,
+ DEFAULT_REALTIME_OPTIONS,
+} from '@supabase/supabase-js/dist/main/lib/constants'
+import { merge, sortBy, take } from 'lodash'
import { Dispatch, SetStateAction, useCallback, useEffect, useReducer, useState } from 'react'
import toast from 'react-hot-toast'
@@ -43,6 +47,7 @@ export interface RealtimeConfig {
token: string
schema: string
table: string
+ isChannelPrivate: boolean
filter: string | undefined
bearer: string | null
enableBroadcast: boolean
@@ -62,6 +67,7 @@ export const useRealtimeMessages = (
token,
schema,
table,
+ isChannelPrivate,
filter,
bearer,
enablePresence,
@@ -76,40 +82,43 @@ export const useRealtimeMessages = (
? `${data.autoApiService.protocol}://${data.autoApiService.endpoint}`
: `https://${projectRef}.supabase.co`
+ const realtimeUrl = `${host}/realtime/v1`.replace(/^http/i, 'ws')
+
const [logData, dispatch] = useReducer(reducer, [] as LogData[])
const pushMessage = (messageType: string, metadata: any) => {
dispatch({ type: 'add', payload: { messageType, metadata } })
}
// Instantiate our client with the Realtime server and params to connect with
- let [client, setClient] = useState<SupabaseClient<any, 'public', any> | undefined>()
+ let [client, setClient] = useState<RealtimeClient>()
let [channel, setChannel] = useState<RealtimeChannel | undefined>()
useEffect(() => {
if (!enabled) {
return
}
- const opts = {
- global: {
- headers: {
- 'User-Agent': `supabase-api/${process.env.VERCEL_GIT_COMMIT_SHA || 'unknown-sha'}`,
- },
- },
- realtime: {
- params: {
- log_level: logLevel,
- },
+
+ const globalOptions = merge(DEFAULT_GLOBAL_OPTIONS, {
+ headers: {
+ 'User-Agent': `supabase-api/${process.env.VERCEL_GIT_COMMIT_SHA || 'unknown-sha'}`,
},
+ })
+ const realtimeOptions = merge(DEFAULT_REALTIME_OPTIONS, { params: { log_level: logLevel } })
+
+ const options = {
+ headers: globalOptions.headers,
+ ...realtimeOptions,
+ params: { apikey: token, ...realtimeOptions.params },
}
- const newClient = new SupabaseClient(host, token, opts)
+ const realtimeClient = new RealtimeClient(realtimeUrl, options)
if (bearer) {
- newClient.realtime.setAuth(bearer)
+ realtimeClient.setAuth(bearer)
}
- setClient(newClient)
+ setClient(realtimeClient)
return () => {
- client?.realtime.disconnect()
+ realtimeClient.disconnect()
setClient(undefined)
}
}, [enabled, bearer, host, logLevel, token])
@@ -120,7 +129,7 @@ export const useRealtimeMessages = (
}
dispatch({ type: 'clear' })
const newChannel = client?.channel(channelName, {
- config: { broadcast: { self: true } },
+ config: { broadcast: { self: true }, private: isChannelPrivate },
})
// Hack to confirm Postgres is subscribed
// Need to add 'extension' key in the 'payload'
diff --git a/apps/studio/package.json b/apps/studio/package.json
index 7577acb527e..100ed2e156d 100644
--- a/apps/studio/package.json
+++ b/apps/studio/package.json
@@ -38,6 +38,7 @@
"@stripe/stripe-js": "^3.0.5",
"@supabase/auth-helpers-react": "^0.4.2",
"@supabase/pg-meta": "*",
+ "@supabase/realtime-js-next": "npm:@supabase/[email protected]",
"@supabase/shared-types": "0.1.65",
"@supabase/supabase-js": "^2.41.1",
"@tanstack/react-query": "4.35.7",
diff --git a/package-lock.json b/package-lock.json
index e17df090917..676b2fb6eda 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1237,6 +1237,7 @@
"@stripe/stripe-js": "^3.0.5",
"@supabase/auth-helpers-react": "^0.4.2",
"@supabase/pg-meta": "*",
+ "@supabase/realtime-js-next": "npm:@supabase/[email protected]",
"@supabase/shared-types": "0.1.65",
"@supabase/supabase-js": "^2.41.1",
"@tanstack/react-query": "4.35.7",
@@ -13675,6 +13676,18 @@
"ws": "^8.14.2"
}
},
+ "node_modules/@supabase/realtime-js-next": {
+ "name": "@supabase/realtime-js",
+ "version": "2.10.0-next.8",
+ "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-2.10.0-next.8.tgz",
+ "integrity": "sha512-mwyvgG6pBqpThgGwZxDQNG7HIwEQgto6ImaLPkGclxyCucTkapCvZiYXQOWrVQKNqe5JiPksNbX+3Ssv7k4xUg==",
+ "dependencies": {
+ "@supabase/node-fetch": "^2.6.14",
+ "@types/phoenix": "^1.5.4",
+ "@types/ws": "^8.5.10",
+ "ws": "^8.14.2"
+ }
+ },
"node_modules/@supabase/shared-types": {
"version": "0.1.65",
"resolved": "https://registry.npmjs.org/@supabase/shared-types/-/shared-types-0.1.65.tgz",