Answer the question
In order to leave comments, you need to log in
How to correctly create a ws connection?
How to correctly create a ws connection?
For educational purposes, I began to get acquainted with aiohtt.
I am doing a pet project where I use VueJs on the front, and aiohttp on the back, respectively.
Faced the task of how to properly make real-time notifications, for example, as it is implemented in VK, when new messages arrive / friends are added / other.
On the front, if the user is authorized, then a ws connection to the server is automatically established.
On the back, there is a ws handler:ws = new WebSocket(URL);
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
request.app['websockets']['user_id'] = ws
async for msg in ws:
pass # корректна ли эта инструкция, чтобы при потери соединения выполнялась строчка ниже?
request.app['websockets'].remove(ws)
return ws
async def new_message(request):
# тут валидируем и сохраняем сообщение
# отправляем данные в сокет
ws = request.app['websockets']['receiver_id']:
ws.send_str('new message detail')
Answer the question
In order to leave comments, you need to log in
In general, everything is correct, only await ws.send_str('new message detail')
instead it is pass
worth adding
if msg.type == WSMsgType.ERROR:
какая-нибудь реакция на ошибку
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question