Skip to content

Commit

Permalink
fix: Fix heartbeat (#248)
Browse files Browse the repository at this point in the history
* fix: fix heartbeat

* fix: fix heartbeat

* fix: fix heartbeat

* fix: fix heartbeat

* fix: fix heartbeat
  • Loading branch information
juancarlospaco authored Dec 23, 2024
1 parent f2f8975 commit 9477bd0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,35 @@ async def _heartbeat(self) -> None:
ref=None,
)
await self.send(data)
await asyncio.sleep(self.hb_interval)
# Use max to avoid hb_interval=0 bugs etc
await asyncio.sleep(max(self.hb_interval, 15))
except websockets.exceptions.ConnectionClosed:
# If ConnectionClosed then is_connected == False
self.is_connected = False

if self.auto_reconnect:
logger.info("Connection with server closed, trying to reconnect...")
await self.connect()
# If auto_reconnect and connect() then is_connected == True
self.is_connected = True

## Apply the new socket to every channel and rejoin.
for topic, channel in self.channels.items():
logger.info(f"Rejoining to: {topic}")
channel.socket = self
await channel._rejoin()
# Wait before sending another phx_join message.
# Use max to avoid hb_interval=0 bugs etc
await asyncio.sleep(max(self.hb_interval, 15))

else:
# If ConnectionClosed and not auto_reconnect then is_connected == False
self.is_connected = False
logger.exception("Connection with the server closed.")
break
else:
# Everything went Ok then is_connected == True
self.is_connected = True

@ensure_connection
def channel(
Expand Down

0 comments on commit 9477bd0

Please sign in to comment.