Skip to content

Commit 9ee9997

Browse files
jeremenichellimandariniCopilot
authored
docs(realtime): enrich docs comment for package (#2164)
Co-authored-by: Katerina Skroumpelou <mandarini@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Katerina Skroumpelou <sk.katherine@gmail.com>
1 parent dff2d04 commit 9ee9997

File tree

4 files changed

+266
-9
lines changed

4 files changed

+266
-9
lines changed

packages/core/realtime-js/src/RealtimeChannel.ts

Lines changed: 213 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ export default class RealtimeChannel {
210210
* The topic determines which realtime stream you are subscribing to. Config options let you
211211
* enable acknowledgement for broadcasts, presence tracking, or private channels.
212212
*
213-
* @example
213+
* @category Realtime
214+
*
215+
* @example Example for a public channel
214216
* ```ts
215217
* import RealtimeClient from '@supabase/realtime-js'
216218
*
@@ -253,7 +255,10 @@ export default class RealtimeChannel {
253255
}
254256
}
255257

256-
/** Subscribe registers your client with the server */
258+
/**
259+
* Subscribe registers your client with the server
260+
* @category Realtime
261+
*/
257262
subscribe(
258263
callback?: (status: REALTIME_SUBSCRIBE_STATES, err?: Error) => void,
259264
timeout = this.timeout
@@ -372,6 +377,8 @@ export default class RealtimeChannel {
372377
*
373378
* The shape is a map keyed by presence key (for example a user id) where each entry contains the
374379
* tracked metadata for that user.
380+
*
381+
* @category Realtime
375382
*/
376383
presenceState<T extends { [key: string]: any } = {}>(): RealtimePresenceState<T> {
377384
return this.presence.state as RealtimePresenceState<T>
@@ -380,6 +387,8 @@ export default class RealtimeChannel {
380387
/**
381388
* Sends the supplied payload to the presence tracker so other subscribers can see that this
382389
* client is online. Use `untrack` to stop broadcasting presence for the same key.
390+
*
391+
* @category Realtime
383392
*/
384393
async track(
385394
payload: { [key: string]: any },
@@ -397,6 +406,8 @@ export default class RealtimeChannel {
397406

398407
/**
399408
* Removes the current presence state for this client.
409+
*
410+
* @category Realtime
400411
*/
401412
async untrack(opts: { [key: string]: any } = {}): Promise<RealtimeChannelSendResponse> {
402413
return await this.send(
@@ -529,6 +540,165 @@ export default class RealtimeChannel {
529540
filter: {},
530541
callback: (payload: any) => void
531542
): RealtimeChannel
543+
/**
544+
* Listen to realtime events on this channel.
545+
* @category Realtime
546+
*
547+
* @remarks
548+
* - By default, Broadcast and Presence are enabled for all projects.
549+
* - By default, listening to database changes is disabled for new projects due to database performance and security concerns. You can turn it on by managing Realtime's [replication](/docs/guides/api#realtime-api-overview).
550+
* - You can receive the "previous" data for updates and deletes by setting the table's `REPLICA IDENTITY` to `FULL` (e.g., `ALTER TABLE your_table REPLICA IDENTITY FULL;`).
551+
* - Row level security is not applied to delete statements. When RLS is enabled and replica identity is set to full, only the primary key is sent to clients.
552+
*
553+
* @example Listen to broadcast messages
554+
* ```js
555+
* const channel = supabase.channel("room1")
556+
*
557+
* channel.on("broadcast", { event: "cursor-pos" }, (payload) => {
558+
* console.log("Cursor position received!", payload);
559+
* }).subscribe((status) => {
560+
* if (status === "SUBSCRIBED") {
561+
* channel.send({
562+
* type: "broadcast",
563+
* event: "cursor-pos",
564+
* payload: { x: Math.random(), y: Math.random() },
565+
* });
566+
* }
567+
* });
568+
* ```
569+
*
570+
* @example Listen to presence sync
571+
* ```js
572+
* const channel = supabase.channel('room1')
573+
* channel
574+
* .on('presence', { event: 'sync' }, () => {
575+
* console.log('Synced presence state: ', channel.presenceState())
576+
* })
577+
* .subscribe(async (status) => {
578+
* if (status === 'SUBSCRIBED') {
579+
* await channel.track({ online_at: new Date().toISOString() })
580+
* }
581+
* })
582+
* ```
583+
*
584+
* @example Listen to presence join
585+
* ```js
586+
* const channel = supabase.channel('room1')
587+
* channel
588+
* .on('presence', { event: 'join' }, ({ newPresences }) => {
589+
* console.log('Newly joined presences: ', newPresences)
590+
* })
591+
* .subscribe(async (status) => {
592+
* if (status === 'SUBSCRIBED') {
593+
* await channel.track({ online_at: new Date().toISOString() })
594+
* }
595+
* })
596+
* ```
597+
*
598+
* @example Listen to presence leave
599+
* ```js
600+
* const channel = supabase.channel('room1')
601+
* channel
602+
* .on('presence', { event: 'leave' }, ({ leftPresences }) => {
603+
* console.log('Newly left presences: ', leftPresences)
604+
* })
605+
* .subscribe(async (status) => {
606+
* if (status === 'SUBSCRIBED') {
607+
* await channel.track({ online_at: new Date().toISOString() })
608+
* await channel.untrack()
609+
* }
610+
* })
611+
* ```
612+
*
613+
* @example Listen to all database changes
614+
* ```js
615+
* supabase
616+
* .channel('room1')
617+
* .on('postgres_changes', { event: '*', schema: '*' }, payload => {
618+
* console.log('Change received!', payload)
619+
* })
620+
* .subscribe()
621+
* ```
622+
*
623+
* @example Listen to a specific table
624+
* ```js
625+
* supabase
626+
* .channel('room1')
627+
* .on('postgres_changes', { event: '*', schema: 'public', table: 'countries' }, payload => {
628+
* console.log('Change received!', payload)
629+
* })
630+
* .subscribe()
631+
* ```
632+
*
633+
* @example Listen to inserts
634+
* ```js
635+
* supabase
636+
* .channel('room1')
637+
* .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'countries' }, payload => {
638+
* console.log('Change received!', payload)
639+
* })
640+
* .subscribe()
641+
* ```
642+
*
643+
* @exampleDescription Listen to updates
644+
* By default, Supabase will send only the updated record. If you want to receive the previous values as well you can
645+
* enable full replication for the table you are listening to:
646+
*
647+
* ```sql
648+
* alter table "your_table" replica identity full;
649+
* ```
650+
*
651+
* @example Listen to updates
652+
* ```js
653+
* supabase
654+
* .channel('room1')
655+
* .on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'countries' }, payload => {
656+
* console.log('Change received!', payload)
657+
* })
658+
* .subscribe()
659+
* ```
660+
*
661+
* @exampleDescription Listen to deletes
662+
* By default, Supabase does not send deleted records. If you want to receive the deleted record you can
663+
* enable full replication for the table you are listening to:
664+
*
665+
* ```sql
666+
* alter table "your_table" replica identity full;
667+
* ```
668+
*
669+
* @example Listen to deletes
670+
* ```js
671+
* supabase
672+
* .channel('room1')
673+
* .on('postgres_changes', { event: 'DELETE', schema: 'public', table: 'countries' }, payload => {
674+
* console.log('Change received!', payload)
675+
* })
676+
* .subscribe()
677+
* ```
678+
*
679+
* @exampleDescription Listen to multiple events
680+
* You can chain listeners if you want to listen to multiple events for each table.
681+
*
682+
* @example Listen to multiple events
683+
* ```js
684+
* supabase
685+
* .channel('room1')
686+
* .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'countries' }, handleRecordInserted)
687+
* .on('postgres_changes', { event: 'DELETE', schema: 'public', table: 'countries' }, handleRecordDeleted)
688+
* .subscribe()
689+
* ```
690+
*
691+
* @exampleDescription Listen to row level changes
692+
* You can listen to individual rows using the format `{table}:{col}=eq.{val}` - where `{col}` is the column name, and `{val}` is the value which you want to match.
693+
*
694+
* @example Listen to row level changes
695+
* ```js
696+
* supabase
697+
* .channel('room1')
698+
* .on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'countries', filter: 'id=eq.200' }, handleRecordUpdated)
699+
* .subscribe()
700+
* ```
701+
*/
532702
on(
533703
type: `${REALTIME_LISTEN_TYPES}`,
534704
filter: { event: string; [key: string]: string },
@@ -550,6 +720,8 @@ export default class RealtimeChannel {
550720
* @param payload Payload to be sent (required)
551721
* @param opts Options including timeout
552722
* @returns Promise resolving to object with success status, and error details if failed
723+
*
724+
* @category Realtime
553725
*/
554726
async httpSend(
555727
event: string,
@@ -611,6 +783,39 @@ export default class RealtimeChannel {
611783
* @param args.event The name of the event being sent
612784
* @param args.payload Payload to be sent
613785
* @param opts Options to be used during the send process
786+
*
787+
* @category Realtime
788+
*
789+
* @remarks
790+
* - When using REST you don't need to subscribe to the channel
791+
* - REST calls are only available from 2.37.0 onwards
792+
*
793+
* @example Send a message via websocket
794+
* ```js
795+
* const channel = supabase.channel('room1')
796+
*
797+
* channel.subscribe((status) => {
798+
* if (status === 'SUBSCRIBED') {
799+
* channel.send({
800+
* type: 'broadcast',
801+
* event: 'cursor-pos',
802+
* payload: { x: Math.random(), y: Math.random() },
803+
* })
804+
* }
805+
* })
806+
* ```
807+
*
808+
* @exampleResponse Send a message via websocket
809+
* ```js
810+
* ok | timed out | error
811+
* ```
812+
*
813+
* @example Send a message via REST
814+
* ```js
815+
* supabase
816+
* .channel('room1')
817+
* .httpSend('cursor-pos', { x: Math.random(), y: Math.random() })
818+
* ```
614819
*/
615820
async send(
616821
args: {
@@ -687,6 +892,8 @@ export default class RealtimeChannel {
687892
/**
688893
* Updates the payload that will be sent the next time the channel joins (reconnects).
689894
* Useful for rotating access tokens or updating config without re-creating the channel.
895+
*
896+
* @category Realtime
690897
*/
691898
updateJoinPayload(payload: Record<string, any>) {
692899
this.channelAdapter.updateJoinPayload(payload)
@@ -700,6 +907,8 @@ export default class RealtimeChannel {
700907
*
701908
* To receive leave acknowledgements, use the a `receive` hook to bind to the server ack, ie:
702909
* channel.unsubscribe().receive("ok", () => alert("left!") )
910+
*
911+
* @category Realtime
703912
*/
704913
async unsubscribe(timeout = this.timeout) {
705914
return new Promise<RealtimeChannelSendResponse>((resolve) => {
@@ -713,6 +922,8 @@ export default class RealtimeChannel {
713922

714923
/**
715924
* Destroys and stops related timers.
925+
*
926+
* @category Realtime
716927
*/
717928
teardown() {
718929
this.channelAdapter.teardown()

0 commit comments

Comments
 (0)