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 anauthenticatecoroutine that returns(bool, client_id)to verify and set client IDs. - Single-connection-per-client: New connections for the same
client_idreplace previous ones (old socket closed, ping task cancelled). - Safer sends: Uses safe JSON serialization and robust error handling during send/broadcast.
custom_fnhook:MongoChangeBroadcasteraccepts acustom_fnto transformChangeEventobjects 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_watchinCollectionConfigto ignore unrelated update events. - Recipient extraction: Support for dot-notation recipient paths (e.g.,
owner.idorfullDocument._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_fnhook, 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
ChangeEventcontent, note thatcustom_fncan 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)