Skip to content

v0.1.6

Latest

Choose a tag to compare

@Youngestdev Youngestdev released this 16 Nov 11:05
· 1 commit to main since this release

Release summary

Improved WebSocket client reliability with ping/pong monitoring and automatic disconnects; added extensibility and resiliency improvements to the core broadcaster (custom hooks, per-collection retry/backoff, field filtering, and recipient extraction).

Highlights

  • WebSocket ping/pong: Added a ping/pong monitor (60s interval) to detect dead clients and update last-pong timestamps.
  • Auto-disconnect on timeout: WebSocketChannel(disconnect_on_timeout=True, timeout=<seconds>) will now drop stale connections.
  • Authentication hook: WebSocketChannel.connect(...) accepts an authenticate coroutine that returns (bool, client_id) to verify and set client IDs.
  • Single-connection-per-client: New connections for the same client_id replace previous ones (old socket closed, ping task cancelled).
  • Safer sends: Uses safe JSON serialization and robust error handling during send/broadcast.
  • custom_fn hook: MongoChangeBroadcaster accepts a custom_fn to transform ChangeEvent objects before they are sent to channels.
  • Retry/backoff on watchers: Per-collection watchers use exponential backoff (via tenacity) to improve resiliency to transient errors.
  • Mongo URI validation: Connection URI is validated before creating the Mongo client.
  • Field-level filtering: Use fields_to_watch in CollectionConfig to ignore unrelated update events.
  • Recipient extraction: Support for dot-notation recipient paths (e.g., owner.id or fullDocument._id) for targeted delivery.
  • Cleaner lifecycle: Improved start/stop behavior and logging for watchers and channels.

Notable files changed

  • mongo_broadcaster/channels/websocket.py — ping/pong monitor, timeout behavior, authentication, replacement of existing connections.
  • mongo_broadcaster/broadcaster.py — custom_fn hook, retry wrapper for watchers, field filtering, recipient extraction, Mongo URI validation.

Upgrade / migration notes

  • If you relied on previous behavior of multiple simultaneous connections with the same client_id, update clients to avoid unexpected disconnects (or set unique IDs).
  • If you run WebSocket endpoints behind a proxy/load-balancer, ensure proxy timeouts are longer than the ping interval or that the ping/pong path is preserved.
  • When using authenticate, ensure the coroutine returns (True, client_id) on success; otherwise connection will be closed.
  • If you depend on raw ChangeEvent content, note that custom_fn can modify events — review hooks in your deployment.

Example usage

from mongo_broadcaster.broadcaster import MongoChangeBroadcaster
from mongo_broadcaster.channels.websocket import WebSocketChannel

ws = WebSocketChannel(disconnect_on_timeout=True, timeout=120)
b = MongoChangeBroadcaster(config, custom_fn)
b.add_channel(ws)