Day 05 Integration & Deployment

Realtime

Subscribe to database changes with Supabase Realtime, build a live chat, and understand broadcast and presence.

~1 hour Hands-on Precision AI Academy

Today’s Objective

Subscribe to database changes with Supabase Realtime, build a live chat, and understand broadcast and presence.

Supabase wraps PostgreSQL's logical replication into a WebSocket API. Changes to any table can broadcast to subscribed clients instantly.

Subscribe to database changes
SUBSCRIBE TO DATABASE CHANGES
import { supabase } from './supabase';

// Listen to all inserts in 'messages' table
const channel = supabase .channel('public:messages') .on( 'postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, (payload) => { console.log('New message:', payload.new); setMessages(prev => [...prev, payload.new]); } ) .subscribe();

// Cleanup on unmount
return () => supabase.removeChannel(channel);
Broadcast (custom events between clients)
BROADCAST (CUSTOM EVENTS BETWEEN CLIENTS)
// Send
await supabase.channel('room:123').send({ type: 'broadcast', event: 'user_typing', payload: { userId: user.id, name: user.name }
});

// Receive
supabase.channel('room:123') .on('broadcast', { event: 'user_typing' }, ({ payload }) => { setTypingUsers(prev => [...prev, payload.name]); }) .subscribe();
Presence (who is online)
PRESENCE (WHO IS ONLINE)
const channel = supabase.channel('room:123');

channel .on('presence', { event: 'sync' }, () => { const state = channel.presenceState(); console.log('Online users:', Object.values(state)); }) .subscribe(async (status) => { if (status === 'SUBSCRIBED') { await channel.track({ userId: user.id, name: user.name }); } });
📝 Day 5 Exercise
Build a Live Chat Room
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. m
  9. e
  10. s
  11. s
  12. a
  13. g
  14. e
  15. s
  16. t
  17. a
  18. b
  19. l
  20. e
  21. .
  22. B
  23. u
  24. i
  25. l
  26. d
  27. a
  28. c
  29. h
  30. a
  31. t
  32. U
  33. I
  34. t
  35. h
  36. a
  37. t
  38. s
  39. e
  40. n
  41. d
  42. s
  43. m
  44. e
  45. s
  46. s
  47. a
  48. g
  49. e
  50. s
  51. t
  52. o
  53. t
  54. h
  55. e
  56. t
  57. a
  58. b
  59. l
  60. e
  61. a
  62. n
  63. d
  64. s
  65. u
  66. b
  67. s
  68. c
  69. r
  70. i
  71. b
  72. e
  73. s
  74. t
  75. o
  76. n
  77. e
  78. w
  79. i
  80. n
  81. s
  82. e
  83. r
  84. t
  85. s
  86. w
  87. i
  88. t
  89. h
  90. R
  91. e
  92. a
  93. l
  94. t
  95. i
  96. m
  97. e
  98. .
  99. A
  100. d
  101. d
  102. a
  103. t
  104. y
  105. p
  106. i
  107. n
  108. g
  109. i
  110. n
  111. d
  112. i
  113. c
  114. a
  115. t
  116. o
  117. r
  118. u
  119. s
  120. i
  121. n
  122. g
  123. B
  124. r
  125. o
  126. a
  127. d
  128. c
  129. a
  130. s
  131. t
  132. .
  133. S
  134. h
  135. o
  136. w
  137. w
  138. h
  139. o
  140. '
  141. s
  142. o
  143. n
  144. l
  145. i
  146. n
  147. e
  148. w
  149. i
  150. t
  151. h
  152. P
  153. r
  154. e
  155. s
  156. e
  157. n
  158. c
  159. e
  160. .

Supporting Resources

Go deeper with these references.

Supabase
Supabase Documentation Complete reference for Auth, Database, Storage, Edge Functions, and Realtime.
GitHub
Supabase Examples Official collection of quickstarts and example apps in multiple frameworks.
YouTube
Supabase YouTube Channel Tutorial videos from the Supabase team covering new features and patterns.

Day 5 Checkpoint

Before moving on, make sure you can answer these without looking:

Course Complete
Return to Supabase in 5 Days Overview