Skip to content

Add pubsub.LiveEventSubscriber (subscribe_to_changes)#7

Merged
winrid merged 1 commit into
mainfrom
add-pubsub-subscribe-to-changes
Jul 10, 2026
Merged

Add pubsub.LiveEventSubscriber (subscribe_to_changes)#7
winrid merged 1 commit into
mainfrom
add-pubsub-subscribe-to-changes

Conversation

@winrid

@winrid winrid commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What

Adds live subscription support to the Python SDK, porting the Java SDK's LiveEventSubscriber / subscribeToChanges. Consumers can now receive real-time comment events (new comments, votes, edits, notifications, etc.) over a WebSocket.

from pubsub import LiveEventSubscriber

subscriber = LiveEventSubscriber()
result = subscriber.subscribe_to_changes(
    tenant_id_ws="YOUR_TENANT_ID",
    url_id="page-url-id",
    url_id_ws="page-url-id",
    user_id_ws="a-unique-presence-id",
    handle_live_event=lambda event: print(event.type),
    region=None,  # "eu" for the EU region
)
# ...later:
result.close()

How

New hand-written pubsub/ module, packaged alongside sso/ (survives client regeneration since update.sh only touches ./client):

  • pubsub/live_event_subscriber.pyLiveEventSubscriber.subscribe_to_changes(...). Runs the connection on a background daemon thread, transparently reconnects with jitter, and fetches events missed while disconnected from the event-log endpoint on reconnect. Optional can_see_comments visibility filter, on_connection_status_change callback, disable_live_commenting short-circuit, and LiveEventSubscriber.create_testing() for a short NAT-friendly ping interval.
  • pubsub/subscribe_to_changes_result.pySubscribeToChangesResult.close().
  • pyproject.toml — new pubsub optional extra (fastcomments[client] + websocket-client>=1.5.0); package discovery and coverage include pubsub; dev extra pulls it in.
  • README.md — usage section + install/import notes.

EU region fix (baked in)

The event-log fallback defaults its REST host from region: when region="eu" and the caller hasn't overridden api_host/api_client, it targets https://eu.fastcomments.com instead of silently querying US infra and getting nothing back. (The Java SDK had the identical bug in its fallback — fixed in FastComments/fastcomments-java#16.)

Testing

tests/test_pubsub.py — 11 unit tests, WebSocket and event-log API mocked (no real network): URL/region routing, message dispatch, can_see_comments filtering, connection-status callback, intentional-close-prevents-reconnect, event-log missed-event replay, and EU/US/override host selection.

pytest tests/test_pubsub.py tests/test_sso.py
33 passed

Note

websocket-client requires Python >= 3.9 while the base package floor is 3.8. It's only pulled in via the optional pubsub extra, so the base/SSO install (floor 3.8) is unaffected.

Port the Java SDK's subscribe_to_changes to Python. New hand-written pubsub/
module (packaged alongside sso/, survives client regeneration since update.sh
only touches ./client) that opens a WebSocket to the FastComments live server,
hands each parsed LiveEvent to a callback, fetches events missed while
disconnected from the event-log endpoint on reconnect, and reconnects with
jitter until the caller closes the subscription.

- pubsub/live_event_subscriber.py: LiveEventSubscriber.subscribe_to_changes
- pubsub/subscribe_to_changes_result.py: SubscribeToChangesResult.close()
- pyproject.toml: new "pubsub" optional extra (client + websocket-client);
  package discovery + coverage include pubsub; dev extra pulls it in
- README: usage section + install/import notes
- tests/test_pubsub.py: unit tests (WebSocket + event-log API mocked)

The EU region event-log fallback uses https://eu.fastcomments.com when
region="eu" (unless api_host/api_client is overridden), so missed-event replay
queries EU infra for EU tenants instead of silently hitting US.
@winrid
winrid merged commit 1d5dd64 into main Jul 10, 2026
1 check passed
@winrid
winrid deleted the add-pubsub-subscribe-to-changes branch July 10, 2026 23:34
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