Connect Autobahn Twisted WSS Through an HTTP Proxy
Let connectWS coordinate the proxy endpoint and later destination TLS instead of opening a direct endpoint beside a proxy-aware factory.
A Twisted endpoint performs the TCP connection. A WebSocket factory configured to negotiate a proxy still needs that endpoint to connect to the intermediary.
For an existing Autobahn application using its standard helper:
from autobahn.twisted.websocket import WebSocketClientFactory, connectWS
from twisted.internet import ssl
factory = WebSocketClientFactory(
'wss://events.example/channel',
proxy={'host': 'proxy.example', 'port': 8080},
)
factory.protocol = ApplicationProtocol
connector = connectWS(
factory,
contextFactory=ssl.optionsForClientTLS('events.example'),
timeout=10,
)
ApplicationProtocol is the application’s existing WebSocket protocol class. Keep reactor and connection cleanup with that application’s lifecycle. For a public WSS service permitted through a public intermediary, the HTTP proxy list supplies candidates for the factory’s proxy host and port.
For a proxy-configured factory, connectWS connects TCP to the proxy and retains the destination TLS context for the later secure connection. It does not initially wrap the ordinary HTTP proxy connection in destination TLS.
A custom HostnameEndpoint and ClientService arrangement must preserve the same division: first connect to the proxy, then let the configured factory negotiate the target and establish its required TLS. Connecting the endpoint directly to events.example while its factory expects a proxy conversation mixes those roles.
The WebSocket URL stays the real service destination. Its hostname also supplies the TLS identity; replacing it with the proxy hostname does not configure the intended secure service.