You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
What are heartbeat messages?
Heartbeat messages are periodic signals sent between the Realtime client and server to verify that the WebSocket connection is alive and functioning properly. The client sends a heartbeat message on the phoenix topic with the event type heartbeat at regular intervals (default: 25 seconds), and the server responds with a phx_reply message.
These messages serve as a keep-alive mechanism to detect connection issues that might not be immediately apparent, such as silent network failures or intermediary proxy timeouts.
Why heartbeats matter
Heartbeat messages are critical for maintaining reliable real-time connections:
Connection health monitoring: They detect when a connection has silently failed without triggering WebSocket close events
Automatic recovery: When heartbeats time out, the client automatically attempts to reconnect
Network proxy compatibility: Many network proxies and load balancers close idle connections; heartbeats keep the connection active
Early issue detection: Heartbeat timeouts alert you to connection problems before users experience message delivery failures
Without heartbeats, your application might appear connected while being unable to send or receive messages.
How to handle heartbeat messages
You can monitor heartbeat lifecycle events using the onHeartbeat method:
import{createClient}from'@supabase/supabase-js'import{useEffect}from'react'functionMyComponent(){constsupabase=createClient(SUPABASE_URL,SUPABASE_KEY)useEffect(()=>{supabase.realtime.onHeartbeat((status)=>{console.log('Heartbeat status:',status)// status can be: 'sent', 'ok', 'error', 'timeout', or 'disconnected'})},[supabase])return<div>Your app content</div>}
Or configure the callback during client initialization:
You can adjust the heartbeat frequency when creating your Supabase client in React:
import{createClient}from'@supabase/supabase-js'import{useMemo}from'react'functionApp(){constsupabase=useMemo(()=>createClient(SUPABASE_URL,SUPABASE_KEY,{realtime: {heartbeatIntervalMs: 15000,// Send heartbeat every 15 seconds (default: 25000)},}),[])return<YourAppsupabase={supabase}/>}
Note: Increasing the interval reduces network traffic but delays detection of connection failures. Decreasing it improves detection speed but increases overhead.
Heartbeat errors in Web workers
For React applications with long-running connections, use Web Workers to prevent browser tab throttling:
import{createClient}from'@supabase/supabase-js'import{useMemo}from'react'functionApp(){constsupabase=useMemo(()=>createClient(SUPABASE_URL,SUPABASE_KEY,{realtime: {worker: true,workerUrl: '/worker.js',// Optional: Place in public folder},}),[])return<YourAppsupabase={supabase}/>}
If the worker fails to start, check:
Browser Web Worker support is available
Worker script is in the public folder and accessible
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
What are heartbeat messages?
Heartbeat messages are periodic signals sent between the Realtime client and server to verify that the WebSocket connection is alive and functioning properly. The client sends a heartbeat message on the
phoenixtopic with the event typeheartbeatat regular intervals (default: 25 seconds), and the server responds with aphx_replymessage.These messages serve as a keep-alive mechanism to detect connection issues that might not be immediately apparent, such as silent network failures or intermediary proxy timeouts.
Why heartbeats matter
Heartbeat messages are critical for maintaining reliable real-time connections:
Without heartbeats, your application might appear connected while being unable to send or receive messages.
How to handle heartbeat messages
You can monitor heartbeat lifecycle events using the
onHeartbeatmethod:Or configure the callback during client initialization:
Troubleshooting heartbeat issues
Frequent heartbeat timeouts
If you're seeing frequent
timeoutstatus in your heartbeat callback:Connection not reconnecting after timeout
The client automatically attempts reconnection with exponential backoff (1s, 2s, 5s, 10s). If you need to manually reconnect:
Customizing heartbeat interval
You can adjust the heartbeat frequency when creating your Supabase client in React:
Note: Increasing the interval reduces network traffic but delays detection of connection failures. Decreasing it improves detection speed but increases overhead.
Heartbeat errors in Web workers
For React applications with long-running connections, use Web Workers to prevent browser tab throttling:
If the worker fails to start, check:
Best practices
Show connection status to users
Display connection status with an indicator:
Reconnect when React Native app comes to foreground
Ensure connection is active when user opens your app:
Monitor connection in your entire app
Set up monitoring when you create your Supabase client:
Beta Was this translation helpful? Give feedback.
All reactions