[{"data":1,"prerenderedAt":36},["ShallowReactive",2],{"blog:post:en:proxy-rotation-server-sent-events-streams":3,"blog:related:en:proxy-rotation-server-sent-events-streams":23},{"slug":4,"lang":5,"title":6,"summary":7,"date":8,"tags":9,"tag_slugs":15,"thumbnail_url":16,"translations":17,"body":18,"asset_base":19,"checksum":20,"published_at":21,"content_updated_at":22},"proxy-rotation-server-sent-events-streams","en","Proxy Rotation for Server-Sent Events Streams","Keep SSE streams alive through rotating proxies: choose sticky mode, handle reconnects with Last-Event-ID, and test rotation deliberately.","2026-11-30",[10,11,12,13,14],"proxy","proxy-rotation","http","networking","python",[10,11,12,13,14],"https://blog-api.ro-proxy.com/api/blog/posts/proxy-rotation-server-sent-events-streams/thumbnail.svg?lang=en",[5],"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.\n\n## Why rotation breaks SSE\n\nAn 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.\n\nTwo other failure modes look like rotation but are not:\n\n- **Buffering.** A proxy or an intermediate server may buffer the response and deliver events in bursts. Use `curl -N` in tests and make sure the origin disables buffering, for example with `X-Accel-Buffering: no` where that header applies.\n- **Idle timeouts.** Proxies, load balancers and origins often close connections that send no bytes for 30-60 seconds. Send a comment heartbeat such as `: ping` every 15-30 seconds.\n\n## Pick the right proxy mode\n\nUse 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.\n\nTimed 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.\n\nFor 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.\n\n## Configure the proxy and verify the exit IP\n\nWith 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:\n\n```bash\ncurl -s -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT https://api.ipify.org?format=json\n```\n\nIn Python, use the same URL for both schemes:\n\n```python\nimport requests\n\nPROXY = 'http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT'\nPROXIES = {'http': PROXY, 'https': PROXY}\n\nr = requests.get('https://api.ipify.org?format=json', proxies=PROXIES, timeout=10)\nr.raise_for_status()\nprint(r.json()['ip'])\n```\n\nIf 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.\n\n## Open and parse the stream\n\nSSE 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.\n\n### Minimal reader\n\n```python\nimport requests\n\nSTREAM_URL = 'https://example.com/events'\nHEADERS = {'Accept': 'text/event-stream', 'Cache-Control': 'no-cache'}\n\nwith requests.get(\n    STREAM_URL,\n    headers=HEADERS,\n    proxies=PROXIES,\n    stream=True,\n    timeout=(10, 300),\n) as r:\n    r.raise_for_status()\n    for raw in r.iter_lines(decode_unicode=True):\n        if not raw:\n            continue\n        if raw.startswith('id:'):\n            last_id = raw[3:].strip()\n        elif raw.startswith('data:'):\n            print(last_id, raw[5:].strip())\n```\n\nThe 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.\n\n## Reconnect with Last-Event-ID\n\nThe 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.\n\n### Reconnect loop\n\n```python\nimport time\nimport requests\n\ndef stream(last_id=None):\n    headers = dict(HEADERS)\n    if last_id:\n        headers['Last-Event-ID'] = last_id\n    with requests.get(\n        STREAM_URL,\n        headers=headers,\n        proxies=PROXIES,\n        stream=True,\n        timeout=(10, 300),\n    ) as r:\n        r.raise_for_status()\n        event_id = last_id\n        for raw in r.iter_lines(decode_unicode=True):\n            if not raw:\n                continue\n            if raw.startswith('id:'):\n                event_id = raw[3:].strip()\n            elif raw.startswith('data:'):\n                yield event_id, raw[5:].strip()\n\nlast_id = None\nbackoff = 1\nwhile True:\n    try:\n        for last_id, data in stream(last_id):\n            print(last_id, data)\n        backoff = 1\n    except requests.RequestException as exc:\n        print('reconnect', exc)\n        time.sleep(backoff)\n        backoff = min(backoff * 2, 30)\n```\n\nAdd 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.\n\n## Test rotation behavior deliberately\n\nDo not wait for a production incident to learn how your stream behaves. Run a controlled test:\n\n1. Open a stream through a sticky proxy and record the exit IP.\n2. Trigger a rotation if your plan allows it, or wait for a timed rotation. Record how long the connection survives.\n3. Reconnect with the last event ID and check for gaps or duplicates.\n4. Repeat with `curl -N` to confirm that events arrive as they are produced, not in a burst at the end.\n5. Add a heartbeat and confirm that an idle stream stays open longer than the proxy idle timeout.\n\nIf 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.\n\n## Operational checklist\n\n- Use sticky mode for streams that carry state or depend on IP affinity.\n- If you use timed mode, set the interval longer than the maximum stream duration.\n- Send a heartbeat comment every 15-30 seconds.\n- Persist the last event ID before processing the event.\n- Reconnect with exponential backoff and jitter.\n- Verify the exit IP on each new connection when identity matters.\n- Keep proxy credentials out of logs and URLs that you share.\n- Test rotation with a real long-lived stream, not a short request.\n\n## Conclusion\n\nSSE 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.\n","https://blog-api.ro-proxy.com/api/blog/posts/proxy-rotation-server-sent-events-streams/assets","e0ee653694ad5987c1254675caf1928be1a342dc7f15d55d4d820a981c32d9f5","2026-11-30T01:03:41.85609Z","2026-11-30T01:03:41.858835Z",[24,28,32],{"slug":25,"title":26,"summary":27},"tunneling-https-through-http-proxy-python","Tunneling HTTPS Through an HTTP Proxy in Python","Learn how to tunnel HTTPS traffic through an HTTP proxy using only Python's standard library, with basic auth and exit IP verification.",{"slug":29,"title":30,"summary":31},"resume-large-downloads-rotating-proxies","Resume Large Downloads Through Rotating Proxies","Learn how HTTP Range requests let you resume interrupted downloads through rotating proxies, and why sticky rotation keeps each byte consistent.",{"slug":33,"title":34,"summary":35},"mastering-http-vs-socks5-proxies-python","Mastering HTTP vs SOCKS5 Proxies in Python for Robust Scraping","Learn the practical differences between HTTP and SOCKS5 proxies, when to pick each, and step‑by‑step code examples for Python scraping projects.",1796251609040]