Proxy Rotation for Server-Sent Events Streams
November 30, 2026
Server-Sent Events (SSE) are a long-lived HTTP response. The client opens one GET request, the server keeps the body open, and both sides exchange small text frames. That shape makes SSE hostile to proxy rotation. A rotating proxy is designed to change the network path; an SSE stream is designed to stay on one path. If you rotate mid-stream, the TCP connection dies and the client must reconnect. The fix is not to avoid rotation. It is to treat rotation as a reconnect boundary and make reconnects cheap and correct.
Why rotation breaks SSE
An SSE response is a single HTTP response with Content-Type: text/event-stream. Over HTTPS, an HTTP proxy does not parse the frames; it opens a CONNECT tunnel and forwards bytes. That means the proxy can keep the stream alive for a long time, but it cannot make a stream survive a change of exit IP. When the proxy rotates, the old TCP connection is closed. The server sees a disconnect, and your client sees an incomplete read.
Two other failure modes look like rotation but are not:
- Buffering. A proxy or an intermediate server may buffer the response and deliver events in bursts. Use
curl -Nin tests and make sure the origin disables buffering, for example withX-Accel-Buffering: nowhere that header applies. - Idle timeouts. Proxies, load balancers and origins often close connections that send no bytes for 30-60 seconds. Send a comment heartbeat such as
: pingevery 15-30 seconds.
Pick the right proxy mode
Use a sticky proxy for the lifetime of a stream. Sticky mode keeps the same IP until you rotate it or the IP fails. That gives the origin a stable client identity and avoids losing server-side state that is tied to the connection or the IP.
Timed mode is still usable, but set the interval longer than your longest expected stream. If the interval is shorter, the proxy will change IP while the stream is open, and the stream will drop. Treat that drop as expected and reconnect. Do not set a one-minute rotation interval for a stream that can last ten minutes and then wonder why events are missing.
For plain HTTP streams, the proxy forwards the request directly. For HTTPS streams, the proxy uses CONNECT. Both work, but HTTPS is easier to reason about because the proxy cannot rewrite the event payload. If you need SOCKS5, use the SOCKS5 port with the matching URL scheme; do not mix schemes in one connection pool.
Configure the proxy and verify the exit IP
With RoProxy, each proxy has its own username and password and connects through a gateway host on an HTTP port used for both HTTP and HTTPS; Pro and Business plans also expose a SOCKS5 port. Build the proxy URL with the placeholders and check the exit IP before you open a long-lived stream:
curl -s -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT https://api.ipify.org?format=json
In Python, use the same URL for both schemes:
import requests
PROXY = 'http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT'
PROXIES = {'http': PROXY, 'https': PROXY}
r = requests.get('https://api.ipify.org?format=json', proxies=PROXIES, timeout=10)
r.raise_for_status()
print(r.json()['ip'])
If the exit IP is not the one you expect, stop. Opening a stream through a misconfigured proxy only hides the problem behind a long timeout.
Open and parse the stream
SSE frames are line-based. An event has optional id:, event: and data: lines, and a blank line ends the frame. For a minimal client, read lines as they arrive and keep the last event ID. Do not call .text or .json() on the response; that waits for the stream to end, which may be never.
Minimal reader
import requests
STREAM_URL = 'https://example.com/events'
HEADERS = {'Accept': 'text/event-stream', 'Cache-Control': 'no-cache'}
with requests.get(
STREAM_URL,
headers=HEADERS,
proxies=PROXIES,
stream=True,
timeout=(10, 300),
) as r:
r.raise_for_status()
for raw in r.iter_lines(decode_unicode=True):
if not raw:
continue
if raw.startswith('id:'):
last_id = raw[3:].strip()
elif raw.startswith('data:'):
print(last_id, raw[5:].strip())
The read timeout is a safety valve, not a rotation strategy. If the server sends a heartbeat every 20 seconds, a 300-second read timeout is generous. If your proxy or origin closes idle connections sooner, lower the heartbeat interval rather than raising the timeout.
Reconnect with Last-Event-ID
The SSE spec gives you a clean recovery mechanism: send the last event ID back in the Last-Event-ID header. The server can then resume from the next event. Your reconnect loop should preserve that ID, back off, and avoid rotating on every failure.
Reconnect loop
import time
import requests
def stream(last_id=None):
headers = dict(HEADERS)
if last_id:
headers['Last-Event-ID'] = last_id
with requests.get(
STREAM_URL,
headers=headers,
proxies=PROXIES,
stream=True,
timeout=(10, 300),
) as r:
r.raise_for_status()
event_id = last_id
for raw in r.iter_lines(decode_unicode=True):
if not raw:
continue
if raw.startswith('id:'):
event_id = raw[3:].strip()
elif raw.startswith('data:'):
yield event_id, raw[5:].strip()
last_id = None
backoff = 1
while True:
try:
for last_id, data in stream(last_id):
print(last_id, data)
backoff = 1
except requests.RequestException as exc:
print('reconnect', exc)
time.sleep(backoff)
backoff = min(backoff * 2, 30)
Add jitter in production so many clients do not reconnect at the same instant. Also verify the exit IP again after a reconnect if you need to know whether the proxy changed. A reconnect through a sticky proxy should usually land on the same IP; a reconnect after an IP failure will not.
Test rotation behavior deliberately
Do not wait for a production incident to learn how your stream behaves. Run a controlled test:
- Open a stream through a sticky proxy and record the exit IP.
- Trigger a rotation if your plan allows it, or wait for a timed rotation. Record how long the connection survives.
- Reconnect with the last event ID and check for gaps or duplicates.
- Repeat with
curl -Nto confirm that events arrive as they are produced, not in a burst at the end. - Add a heartbeat and confirm that an idle stream stays open longer than the proxy idle timeout.
If the stream drops exactly at the rotation interval, that is expected. If it drops before the interval, look for buffering, an idle timeout, or an origin-side limit. If it never drops but events arrive late, the problem is buffering, not rotation.
Operational checklist
- Use sticky mode for streams that carry state or depend on IP affinity.
- If you use timed mode, set the interval longer than the maximum stream duration.
- Send a heartbeat comment every 15-30 seconds.
- Persist the last event ID before processing the event.
- Reconnect with exponential backoff and jitter.
- Verify the exit IP on each new connection when identity matters.
- Keep proxy credentials out of logs and URLs that you share.
- Test rotation with a real long-lived stream, not a short request.
Conclusion
SSE and proxy rotation can coexist if you stop treating a stream as a permanent connection. A stream is a sequence of connections with a resume point. Keep the proxy sticky for each connection, rotate between connections, and make Last-Event-ID part of your normal reconnect path. If you use RoProxy, pick sticky mode for streams and rotate between streams, not during one.