python-socketio icon indicating copy to clipboard operation
python-socketio copied to clipboard

Providing config params to asynchronous drivers

Open svepe opened this issue 4 years ago • 3 comments

It took me a while to figure out why some of my messages were not received by the server and caused connection drop without any errors. Eventually, I found out that aiohttp puts a default limit of the message size to 4MB, however I need to send messages ranging from 1KB to 10MB. The entire communication happens only on localhost so I am not worried about lost connection and chunking will only add unnecessary code overhead. The only way I have found out to configure the max_msg_size of the aiohttp server is the following

sio = AsyncServer(async_mode="aiohttp")
app = web.Application()
sio.attach(app)

class UnlimitedMessageSizeWebsocket(sio.eio._async["websocket"]):
    async def __call__(self, environ):
        request = environ["aiohttp.request"]
        self._sock = WebSocketResponse(max_msg_size=0)
        await self._sock.prepare(request)
        self.environ = environ
        await self.handler(self)
        return self._sock


sio.eio._async["websocket"] = UnlimitedMessageSizeWebsocket

which is not great. My biggest concern is that any changes in the async_driver code will break it since I have literally copied the call function implementation and just added the max_msg_size=0 argument. Is there any better way of doing this or are there any plans to support some sort of config params for the underlying driver?

Furthermore, aiohttp does seem to throw an error when a package is larger than the max size, but it is silently caught somewhere within socketio or engineio which is not great. Perhaps just printing the error message will save lots of time when dealing with issues caused by the underlying server.

svepe avatar May 18 '20 14:05 svepe

Okay, I need to think about how to expose these driver-specific options.

miguelgrinberg avatar May 18 '20 15:05 miguelgrinberg

Thanks for working this out @svepe

I spent half of the day analyzing the same error when sending messages larger than 4MB with aiohttp:

Unexpected error decoding packet: "'WebSocketError' object is not subscriptable", aborting

Finally I added max_msg_size=0 to engineio's async client call to ws_connect(...) to overcome this problem.

So I would really appreciate having the option to pass this parameter while creating async client.

damoit avatar Nov 30 '21 00:11 damoit

@damoit Not a good idea to allow packets of unlimited size. You should always have a maximum to prevent abuse and denial of service attacks.

miguelgrinberg avatar Nov 30 '21 20:11 miguelgrinberg