DEV Community

Emily
Emily

Posted on

Handling Real-Time HK Stock Quotes Without Timeouts: A WebSocket Approach

If you’re building any kind of HK stock market tool, you’ve probably hit the wall where your HTTP requests start timing out—especially during the opening rush. In this post, I’ll walk through the exact steps I took to stabilize a brokerage advisory dashboard, moving from fragile polling to a robust WebSocket pipeline.

1. Understanding the Problem

Timeout errors usually stem from two places: the data provider’s gateway being overloaded (leading to actual network timeouts), and your local processing being too slow (causing back-pressure that starves the socket). When I profiled my own app, I realized that synchronous database writes inside the callback were delaying the event loop just enough to trigger downstream timeouts.

2. Stop Polling, Start Streaming

The first architectural change is to replace periodic HTTP pulls with a persistent WebSocket connection. Once established, the server pushes each tick in real time. For HK stocks, I rely on AllTick’s WebSocket feed—it accepts a subscription list and delivers a continuous stream, removing the need for constant reconnections.

3. Code Example: WebSocket Subscription in Python

Here’s a minimal working example that subscribes to Tencent and Alibaba HK shares:

import websocket
import json

def on_message(ws, message):
    data = json.loads(message)
    print(data)  # Received tick data; in production, push to a queue or DB

def on_open(ws):
    subscribe_data = {
        "action": "subscribe",
        "symbols": ["00700.HK", "09988.HK"]  # Symbols to subscribe to
    }
    ws.send(json.dumps(subscribe_data))

ws = websocket.WebSocketApp(
    "wss://api.alltick.co/ws/stock",
    on_message=on_message,
    on_open=on_open
)
ws.run_forever()
Enter fullscreen mode Exit fullscreen mode

Keep the callback light; just parse and forward.

4. Batch Subscriptions and Throttle Requests

When subscribing to many instruments at once, split the list into chunks of 10–20 and insert a 50–100 ms pause between chunks. This prevents the initial burst from overwhelming the server’s TCP buffer and avoids throttling.

5. Implementing Reconnection with Exponential Backoff

Even WebSocket connections drop. I set up a heartbeat (ping every 30s, reconnect after two missed pongs). Reconnection attempts follow an exponential backoff: 1s, 2s, 4s, … max 30s. If only a single symbol fails, I re-subscribe just that one to avoid disturbing the rest.

6. Offload Processing with Async and Queues

Move any heavy work—indicator computation, database writes—out of the on_message thread. I use a queue.Queue and a consumer thread that flushes data in batches. Bulk inserts (every 100 ticks or 200ms) keep the database happy and eliminate write-induced timeouts.

7. Final Tips

Since moving to this WebSocket + queue architecture, the advisory dashboard I maintain hasn’t experienced a single client-visible timeout during trading hours. Start by swapping your transport, then optimize your processing, and your HK stock data pipeline will become orders of magnitude more reliable.

Top comments (0)