Skip to content

feat: add WebSocketClient.set_subscriptions() and resubscribe() - #22

Draft
chipkent wants to merge 1 commit into
feat/ws-immediate-subscriptionsfrom
feat/ws-set-subscriptions
Draft

chipkent wants to merge 1 commit into
feat/ws-immediate-subscriptionsfrom
feat/ws-set-subscriptions

Conversation

@chipkent

@chipkent chipkent commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Preview only. This is a draft in the fork showing the exact text of the PR planned for massive-com/client-python. It will be closed, not merged, once the upstream PR is opened.

Fixes #<issue: F1c>

Builds on #21.

Two new WebSocketClient methods, both safe to call from any thread while connect() runs:

  • set_subscriptions(desired) replaces the wanted topics in one step. The client then sends one unsubscribe message for the removed topics, followed by one subscribe message for the added ones. Removals go first, so a swap made at the server's subscription limit ([SPT-1644] Log WebSocket server errors instead of dropping them massive-com/client-python#1039 logs Subscription limit reached for feed) never asks for more than the limit. Data that both a removed and an added topic cover (e.g. T.AAPL replaced by T.*) is not delivered in between.
  • resubscribe(*topics) sends a subscribe message again for topics the client already counts as subscribed, for example ones the server never acknowledged. A topic that is neither subscribed nor scheduled raises ValueError, and then nothing is sent.

subscribe(), unsubscribe() and unsubscribe_all() keep their send order: subscribe first, then unsubscribe.

client.set_subscriptions(["T.AAPL", "T.NVDA"])  # was T.AAPL, T.MSFT
# sends: unsubscribe T.MSFT, then subscribe T.NVDA
client.resubscribe("T.AAPL")  # sends: subscribe T.AAPL

Tests

  • New test_set_subscriptions.py checks the messages both methods send, and that ValueError sends nothing.
  • make test and make lint pass on 3.10 and 3.14.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

Two things a long-running client needs were not possible:

- Replacing one set of topics with another, removals first. The
  server enforces a per-connection subscription limit (it answers
  "Subscription limit reached for feed", which the client logs since
  massive-com#1039). The client always sends the subscribe message before the
  unsubscribe message, so a swap made at the limit asks for more topics
  than the limit allows before the unsubscribe arrives.
- Sending a subscribe message again for a topic the server never
  acknowledged. subscribe() sends only topics missing from subs, so a
  topic already sent can never be sent twice.

set_subscriptions(desired) replaces the scheduled topics in one step.
The next update then sends one unsubscribe message for the removed
topics, followed by one subscribe message for the added ones.

resubscribe(*topics) adds topics to the next subscribe message. A topic
that is neither subscribed nor scheduled raises ValueError, and then
nothing is sent.

subscribe(), unsubscribe() and unsubscribe_all() keep their order:
subscribe first, then unsubscribe.
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