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 3 commits into
base: master
Choose a base branch
from

Conversation

0xferit
Copy link

@0xferit 0xferit commented Jul 27, 2025

Expose WebSocket heartbeat events

This PR exposes WebSocket heartbeat events to allow users to monitor connection health for both public and authenticated channels.

Changes

  • Added heartbeat event to event emitter: Registered as a valid event type in BfxEventEmitter
  • Exposed heartbeat events: Modified WebSocket client and bucket to emit heartbeat events instead of silently discarding them
  • Consistent handler signature: Heartbeat handlers receive subscription: Optional[Subscription] parameter
    • For public channels: receives subscription details
    • For authenticated connection (channel 0): receives None
  • Added comprehensive example: New examples/websocket/heartbeat.py demonstrates usage for both public and authenticated flows
  • Updated documentation: Added heartbeat events section to README with proper type hints

Handler Signature

@bfx.wss.on("heartbeat")
def on_heartbeat(subscription: Optional[Subscription]) -> None:
    if subscription:
        # Public channel heartbeat
        print(f"Heartbeat for {subscription['channel']}")
    else:
        # Authenticated connection heartbeat
        print("Heartbeat on channel 0")

@0xferit 0xferit force-pushed the master branch 2 times, most recently from b651103 to 714c87c Compare August 10, 2025 21:50
Exposes WebSocket heartbeat events to allow users to monitor connection health
for both public and authenticated channels.

Changes:
- Add 'heartbeat' event to BfxEventEmitter as a valid event type
- Emit heartbeat events in WebSocket client and bucket instead of silently discarding
- Consistent handler signature: receives Optional[Subscription] parameter
  - Public channels: subscription details
  - Authenticated (channel 0): None
- Add comprehensive example with proper type hints and error handling
- Update README with clear documentation and usage examples

The handler signature is consistent across all subscription-based events,
always passing the subscription as first parameter (None for authenticated).

Lines of code: +101 insertions, -4 deletions (+97 net)
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