Day 5 of 5
⏱ ~60 minutes
Supabase in 5 Days — Day 5

Realtime

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

Supabase Realtime

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

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)
// 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)
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. .

Day 5 Summary

  • postgres_changes subscription fires when rows are inserted, updated, or deleted.
  • Broadcast sends custom events between clients in a channel — no database write needed.
  • Presence syncs who is connected to a channel — ideal for online/offline indicators.
  • Always remove channels on component unmount to prevent memory leaks.
Finished this lesson?