I was checking the stability of websocket connection and had kept the data retrieval and processing on for 85 symbols. At 13:21:03 today(29-Sep '23) I got stopped out of the connection with following message.
File “/usr/lib/python3.11/asyncio/streams.py”, line 727, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes
The above exception was the direct cause of the following exception:
.—
websockets.exceptions.ConnectionClosedError: no close frame received or sent
Why this error and is there a workaround? I was running script on Google Cloud
# Create default SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
# Configure OAuth2 access token for authorization
configuration = upstox_client.Configuration()
api_version = '2.0'
configuration.access_token = token
# 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(1) # Wait for 1 second
# Data to be sent over the WebSocket
data = {
"guid": "someguid",
"method": "sub",
"data": {
"mode": "full",
"instrumentKeys": scrips
}
}
# 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:
message = await websocket.recv()
decoded_data = decode_protobuf(message)
# Convert the decoded data to a dictionary
data_dict = MessageToDict(decoded_data)
process_ticks_Upstox.delay(data_dict)
I have kept the code snippet close to the one provided in examples. process_ticks_Upstox.py is a celery job which uses Redis as broker. token is access_token assigned in the main module. scrips is a comma separated list of scrips for which need the streaming data. I had established connection at 9 am today and it was running okay till 13:21 when faced the error.
i also faces same kind of problem
is there any way to get disconnection callback then can able to reconnect to websocket
websockets always should have listners like OnConnect OnCloseConnection OnReconnection like will be there but here we just using raw socket if listners are available what is the method names used here
please resolve this thing
No issues today. Executed perfectly from 09:00 to 15:30. Did not make any changes in the program. Please let me know how can we catch the error and reconnect in case it reappears.
We implemented the CDN configuration over the last week, 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.
The disconnect experienced was due to scheduled technical maintenance on our end and was anticipated. Please be assured that this is not a recurring issue.