Skip to content

Add support for heartbeat events in WebSocket client #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

0xferit
Copy link

@0xferit 0xferit commented Jul 27, 2025

Description

This PR refactors the WebSocket client to expose heartbeat events to library users. Previously, heartbeat messages sent by the Bitfinex WebSocket server were silently consumed and filtered out, preventing users from monitoring connection health or implementing custom logic based on heartbeat patterns. Addressing #252 .

The implementation now:

  • Emits heartbeat events for both public subscriptions and authenticated connections
  • Provides subscription context for public channel heartbeats
  • Maintains clean separation between subscription-based and authenticated heartbeats
  • Preserves all existing functionality without breaking changes

Motivation and Context

Heartbeat events are essential for:

  1. Connection Health Monitoring: Users can track if the WebSocket connection is alive and receiving data
  2. Custom Reconnection Logic: Applications can implement sophisticated reconnection strategies based on heartbeat patterns
  3. Analytics and Debugging: Developers can monitor connection quality and diagnose network issues
  4. Transparency: Users gain full visibility into WebSocket communication

Currently, these events were being consumed internally but not exposed, limiting users' ability to implement robust connection management.

Related Issue

PR addresses the need for exposing heartbeat events that were previously only consumed internally.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Usage Examples

Public Channel Heartbeats

@bfx.wss.on("heartbeat")
def on_heartbeat(subscription):
    print(f"Heartbeat for {subscription['channel']} - {subscription['symbol']}")

- Expose heartbeat events to library users instead of silently consuming them
- Add 'heartbeat' event to allowed events list in BfxEventEmitter
- Emit heartbeat events with subscription info for public channels
- Emit heartbeat events without subscription info for authenticated connections
- Add comprehensive documentation for heartbeat event usage
- Include example implementations for both public and authenticated heartbeats

This allows users to monitor WebSocket connection health and implement
custom reconnection or analytics logic while maintaining full backward
compatibility with existing functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant