Websocket for streaming, last few messages missing

I have a websocket endpoint I am using for streaming some response to a client. I am seeing that sometimes the last few messages sent to client using ws.send() are not reaching the client. I can’t figure out why this is happening.

Sample code has been shown below:

async def stream_endpoint(request, ws):
    data = await ws.recv()
    data = json.loads(data)
    chunk_generator = my_async_generator(data)
    async for chunk in chunk_generator:
        await ws.send(json.dumps(chunk))
    await ws.close()

I noticed that ws.send() is not immediately sending it’s response to the client. The client is receiving responses in groups of a few. If I add an asyncio.sleep(2) after ws.send() call, client receives each chunk one by one. I can’t understand why this is happening.

Is there some sort of buffer in ws.send()?

Is it possible that ws.close() closes the websocket connection before the last few chunks have been sent to the client?