avanza icon indicating copy to clipboard operation
avanza copied to clipboard

Fail to connect to streaming data

Open exallon72 opened this issue 10 months ago • 3 comments

Hi

I am trying to run the example to get the OMXS30 data through the websocket. It logs in fine and I can use the "http" functions just fine.

This works fine:

    certs = avanza.search_for_certificate("BULL OMX X2 AVA")
    print(certs)

However when I get to use websocket, it will timeout without connection.

    asyncio.get_event_loop().run_until_complete(
        subscribe_to_channel(avanza)
    )
    asyncio.get_event_loop().run_forever()

This function will always timeout

    async def __wait_for_websocket_to_be_connected(self):
        timeout_count = 40
        timeout_value = 0.250

        # Waits for a maximum of 10 seconds for the connection to be complete
        for _ in range(0, timeout_count):
            if self._connected:
                return
            await asyncio.sleep(timeout_value)

        raise TimeoutError(
            "\
            We weren't able to connect \
            to the websocket within the expected timeframe \
        "
        )

Does anyone know what is wrong?

exallon72 avatar Feb 03 '25 12:02 exallon72

I seems like this is because of the websocket library being to new.

websockets==10.4 seem to work.

However even if I am connected there is no Quotes coming for OMXS30. Tried with trades for Volvo. Same thing.

exallon72 avatar Feb 03 '25 12:02 exallon72

It seems like there is sometimes a certificate problem. I don't know why it worked intermittently. But to make it always connect to the socket I added.

import ssl
async def __create_socket(self):
        print("disable cert")
        try:
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
            ssl_context.check_hostname = False
            ssl_context.verify_mode = ssl.CERT_NONE
        except Exception as e:
            print(e)
        print("Call socket connect")

exallon72 avatar Feb 04 '25 09:02 exallon72

Version 15.1.0 increases the minimum required version of the websockets dependency to 11. When I tested it was the lowest major version where the current code worked without an issue.

Please check out the new version and say if it solved your problem.

Qluxzz avatar Feb 20 '25 20:02 Qluxzz