Skip to content

How WebSocketFeed works

WebSocketFeed is one WebSocket connection as a WorkManager worker: the platform's way in for venue streams (workmanager#103). The Runtime gives it a lifecycle (StartAsync, StopAsync), an activation lease and an outbox, so the feed is an ordinary worker in ListWorkers, with tags, lifecycle events, dead letters and worker secrets.

A feed serves topics. It is configured with the topics it fills (topics, changed at runtime by the serve command) and maps each one to a venue item with its topicitems rules, which a venue preset renders from the venue's naming fragment. It knows no consumer: a worker that needs market data declares its input topics and subscribes to them, nothing more. Who opens which feed is decided outside the platform, by an orchestrator (a notebook, a script, the TUI) that resolves the topics its workers listen to into feeds (market-data-devkit's FeedResolver) and applies them (FeedOrchestrator: create or serve by a deterministic name, delete what it opened). Workers and feeds start in any order.

How a frame travels

flowchart LR
    venue[(Venue<br/>wss://...)]
    subgraph runtime[WorkManager Runtime pod]
        subgraph feed[WebSocketFeed worker]
            session[WebSocketSession<br/>login, subscribe,<br/>receive, reconnect]
            router[PublishRouter<br/>key_path, keys,<br/>split_arrays]
            queue[[frame queue<br/>one batch per burst]]
        end
        gate{lease<br/>gate}
        outbox[[outbox]]
    end
    api[API gateway<br/>Pubsub]
    consumers[Workers<br/>BinanceOrderBook,<br/>AlpacaOrderBook, ...<br/>subscribed to their topics]
    tui[Orchestrator<br/>notebook, script, TUI]
    wm[WorkManager]

    venue -- frames --> session
    session -- one JSON value --> router --> queue
    queue -- ctx.PublishAsync --> gate --> outbox --> api
    api -- served topic<br/>by routing key --> consumers
    tui -- CreateWorker, InvokeWorkerCommand<br/>serve, send, status --> wm --> feed
    session -- send, subscribe --> venue
    feed -. ReportStatusAsync<br/>connected, reconnecting, ... .-> wm
  1. Start. The Runtime claims the worker's lease and calls StartAsync. The feed parses its config, checks the URL against the SSRF guard, reads its saved session (publish topic, routing, replayed frames, served topics) and starts the session in the background, reporting connecting.
  2. Open. The session connects and runs the open handler before anything is relayed: the login (handshake steps, their secrets resolved now), the derived subscription (the subscribeframe template over the items of the served topics), the fixed subscribe frames. Then it reports connected.
  3. Frames. The receive loop hands every whole message to the feed. A frame that is one JSON value is routed (its routing key names its served topic) and queued; one publisher task hands everything queued to ctx.PublishAsync in one batch, into the Runtime's outbox, which publishes only while the lease is current.
  4. Drop. A dropped socket reports reconnecting and is reopened with backoff (1 s doubling to 30 s), running the login and the subscriptions again. It keeps trying; reconnectmaxattempts caps it.
  5. Stop. Ownership ends (stop, delete, reload, a lost lease, a move): the stop token fires and the feed publishes nothing more; StopAsync sends a normal close and aborts within its deadline. The next activation, on this pod or another, finds the saved session and opens as a handoff: a rejected login (a venue with one connection per account refuses the new socket while the old one still holds the slot) is retried with backoff.
  6. Release (opt-in, releasewhenidleminutes). A feed that has served no topic for that many minutes closes its socket and deletes itself. Without the key it lives until its orchestrator deletes it, idle and connected when it serves nothing.

The worker and its library

WebSocketFeed is the worker; WebSocketDevKit is the library it is built on. The split keeps the worker generic: the library holds everything a connection needs (the session with its reconnects and handoffs, the login steps, frame templates, content routing, the URL guard), and the worker adds what makes it a platform worker (its config and presets, commands, state, publishing through its host). Neither contains a venue's code: a venue's protocol is a preset of the worker's config.

The library also ships the worker's names (the virtufin.worker.websocketfeed fragment), in C# and in Python, so any client knows the feed's topics and types without depending on the worker. A worker that must own a connection itself can use the same library: Own a WebSocket connection in a worker.

Its config, commands, events and templates: WebSocketFeed.