Frequent ConnectionClosed issues with Websocket today

There have been atleat two instances of websocket connection drop today. The reasons are also different. Is there some testing going on from your end? Shall we initiate trades based on websocket streaming or is it advisable to avoid till the testing (if on) is over.

1 Like

@prsh

Currently, we aren’t conducting any tests related to websockets. Please provide your user ID, and we’ll look into the issue.

Thank you!

Thanks for the quick response. My user ID is AQ6555

Same issue, WebSocket connection is getting closed continuously. With error Error :- received 1000 (OK); then sent 1000 (OK)

Complete traceback of the error: -
Traceback (most recent call last):
File “/home/ubuntu/tradingstrategy/scripts/new-api/market-trail-socket.py”, line 156, in
asyncio.run(fetch_market_data())
File “/usr/lib/python3.10/asyncio/runners.py”, line 44, in run
return loop.run_until_complete(main)
File “/usr/lib/python3.10/asyncio/base_events.py”, line 646, in run_until_complete
return future.result()
File “/home/ubuntu/tradingstrategy/scripts/new-api/market-trail-socket.py”, line 109, in fetch_market_data
message = await websocket.recv()
File “/home/ubuntu/virtualenv/tradingstrategy/lib/python3.10/site-packages/websockets/legacy/protocol.py”, line 568, in recv
await self.ensure_open()
File “/home/ubuntu/virtualenv/tradingstrategy/lib/python3.10/site-packages/websockets/legacy/protocol.py”, line 944, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)

@rushi1192 @prsh

To resolve the websocket connection interruptions, we are enhancing the CDN configuration to secure a more reliable connection. Moreover, we’re incorporating a heartbeat protocol (often referred to as “ping/pong”) to keep connections active as long as they are connected.

This approach is designed to ensure users experience a stable connection throughout the entire duration of the socket’s activity.

We will inform you about the progress and let you know when these improvements are deployed.

I used to think there is some issue with my implementation. Thanks to this thread see that it is a common issue. Looking forward to the resolution.

Hello,

I am also facing this issue on daily basis.

I also face problem for getting positions data. For some time I dont get position data even there are current positions.

@shanmu what are the updates on the websocket connections. Can you please tell the approximate time to make deployment.

We implemented the CDN configuration over the weekend, and we are currently monitoring it. We will make any necessary adjustments based on our observations. Kindly inform us if there have been any improvements in the WebSocket connection.

@Pradeep_Jaiswar once the connection is established it does not provide any data in market hours. It jus gets hang after establishing the connection. The program works fine in off market hours but in market trading hours the websocket connection is not providing any data. Have you implemented some sort of rate implementation in websockets also?

def getPortfolioData():
data = {}
try:
portfolio_api_response = portfolio_api_instance.get_positions(api_version)

    for position in portfolio_api_response.data:
        data[position.instrument_token] = {
            "quantity": position.quantity,
            "product":position.product,
        }
except:
    data = None
return data

def get_market_data_feed_authorize(api_version, configuration):
“”“Get authorization for market data feed.”“”
api_instance = upstox_client.WebsocketApi(
upstox_client.ApiClient(configuration))
api_response = api_instance.get_market_data_feed_authorize(api_version)
return api_response

def decode_protobuf(buffer):
“”“Decode protobuf message.”“”
feed_response = pb.FeedResponse()
feed_response.ParseFromString(buffer)
return feed_response

async def fetch_market_data():
“”“Fetch market data using WebSocket and print it.”“”

# Create default SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE


# Get market data feed authorization
response = get_market_data_feed_authorize(
    api_version, configuration)

# Connect to the WebSocket with SSL context
async with websockets.connect(response.data.authorized_redirect_uri, ssl=ssl_context) as websocket:
    print('Connection established')

    await asyncio.sleep(10)  # Wait for 1 second
    # time.sleep(1)  # Wait for 1 second

    # Data to be sent over the WebSocket
    data = {
        "guid": "someguid",
        "method": "sub",
        "data": {
            "mode": "full",
            "instrumentKeys": ["NSE_INDEX|Nifty Bank", "NSE_INDEX|Nifty 50"]
        }
    }

    # # Convert data to binary and send over WebSocket
    # binary_data = json.dumps(data).encode('utf-8')
    # await websocket.send(binary_data)

    # Continuously receive and decode data from WebSocket

    while True:
        pf_data = getPortfolioData()


        if pf_data is None:
            continue

        data = {
            "guid": "someguid",
            "method": "sub",
            "data": {
                "mode": "option_chain",
                "instrumentKeys": list(pf_data.keys())
            }
        }

        # Convert data to binary and send over WebSocket
        binary_data = json.dumps(data).encode('utf-8')
        await websocket.send(binary_data)
        message = await websocket.recv()
        decoded_data = decode_protobuf(message)

        # Convert the decoded data to a dictionary
        data_dict = MessageToDict(decoded_data)
        
        # Print the dictionary representation
        for instrument in data_dict['feeds'].keys():
            option_data = data_dict['feeds'][instrument]
            
            if len(option_data['oc']['optionGreeks']) == 0:
                continue
            delta = option_data['oc']['optionGreeks']['delta']

            print(f'{datetime.now()} => {instrument} => {delta}')

please look into this as this is causing a great trouble for me. Its been a long time I am facing this issue please look into this as soon as possible.

@rushi1192

I tested the websocket connection using the payload you shared:

{
        "guid": "someguid",
        "method": "sub",
        "data": {
            "mode": "full",
            "instrumentKeys": ["NSE_INDEX|Nifty Bank", "NSE_INDEX|Nifty 50"]
        }
    }

The connection worked smoothly, and I received market updates continuously without any issues. It’s important to note that we do not have any rate limit configuration that would restrict instantaneous updates from being streamed. The problem of not receiving data appears to be specific to your code. I recommend you to check your code thoroughly and try again.

Thanks!

@shanmu
This is not my payload. I am continuosly updating my payload in while True loop:

pf_data = getPortfolioData()

    if pf_data is None:
        continue

    data = {
        "guid": "someguid",
        "method": "sub",
        "data": {
            "mode": "option_chain",
            "instrumentKeys": list(pf_data.keys())
        }
    }

    # Convert data to binary and send over WebSocket
    binary_data = json.dumps(data).encode('utf-8')
    await websocket.send(binary_data)

the option_chain the major issue is that it sometimes works fine but sometimes it wont

I think you should pay serious attention to @rushi1192 's message. I am an investor and trader on upstox platform for quite a long time and @rushi1192 is working for me to develop an option trade execution platform. Before we started this project we were in regular discussion with upstox team. I am feeling a bit frustrated by the delay in this project. I think your api and websockets should be more smooth aand user friendly.

Yes definitely, there are issues with websocket connections.

I am getting delayed events which is causing issues.

Upstox team - please fix this asap.

thanks.

We remain dedicated to the ongoing enhancement of the Upstox API, consistently focusing on its improvement. @Ashu_G, could you please provide your order number details? Kindly include when you placed the order and when you received the order update.

once it disconnect it wont reconnect automatically