infinyon / http-source-connector

Official Infinyon HTTP Source Connector
https://www.fluvio.io/connectors/inbound/http/
Apache License 2.0
7 stars 7 forks source link

Pass headers to websocket requests #244

Closed benmanns closed 1 month ago

benmanns commented 1 month ago

This updates the WebSocket connector to pass headers (if provided) the same way that the connector does for HTTP polling/streaming connections do. It's slightly different in that the WebSocket source stores a tungstenite::handshake::client::Request built via tokio_tungstenite::tungstenite::client::IntoClientRequest which automatically fills in necessary headers (namely sec-websocket-key).

I find this functionality necessary to connect to some third-party websocket servers, particularly Kalshi's WebSocket Market Data Feed, which requires a bearer token in an Authorization header to connect.

For reference, my connection config for Kalshi now looks like:

meta:
  version: latest
  name: kalshi-websocket-connector
  type: websocket-source
  topic: {TOPIC}
http:
  endpoint: wss://trading-api.kalshi.com/trade-api/ws/v2
  headers:
    - "Authorization: Bearer {KALSHI_API_TOKEN}"
  websocket_config:
    subscription_message: '{"id":1,"cmd":"subscribe","params":{"channels":["ticker","trade","fill","market_lifecycle"]}}'