Manage websockets across multiple servers workers

0 votes

aiohttp has built-in support for websockets. It's very simple and works well.

A simplified version of the example in the docs is:

async def handler(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    # Async iterate the messages the client sends
    async for message in ws:
        ws.send_str('You sent: %s' % (message.data,))

    print('websocket connection closed')

In the example, ws is a reference to a websocket connection with a client. I could easily put this references into request.app, like @Crandel does here (i.e., global state), but not in a production app, because each app server (and even each worker) will have its own app instance.

Is there an accepted pattern for this? Is there another way?

Note: I'm not referring to sessions. I'm referring to connections. I want to send a message to clients that connected to server A when events occur in application code in server B, etc.

Sep 4, 2018 in Python by bug_seeker
• 15,510 points
4,520 views

1 answer to this question.

0 votes

So I am only familiar with Socket.IO in Node but it's fairly easy to scale websockets horizontally with Socket.IO.

Sockets can come with Sessions, so each session is managed by a specific server. This makes it easy to save state for each socket that is open, and load balance across all of your servers.

Here is SocketIO for Python:

https://pypi.python.org/pypi/socketIO-client

Here is a really good read on how to attach sessions to a redis-store to make it even faster and load balancing across servers more manageable.

How to share sessions with Socket.IO 1.x and Express 4.x?

I know this doesn't answer your question about aiohttp, but hopefully this will give you a better idea about how sockets can work.

Edit: Written in Node-

In Socket.IO this is really easy, it has a ton of functions to broadcast messages in a variety of different ways.

For your example if you would like to emit a message across to everyone in each chat-room. Example everyone that has a socket open you can easily just write.

socket.broadcast.emit('WARNING', "this is a test");

Let's say you have rooms open you can broadcast messages only to people in that room with a simple function called .to(). Example I have a room named 'BBQ':

socket.broadcast.to('BBQ').emit('invitation', 'Come get some food!');

This will message everyone in channel BBQ - Come get some food!

Edit: Edit:

This is a fantastic write for how Socket.IO works, make sure you read the second answer for the updated versions of the functions. It is much easier to read than their documentation.

Send response to all clients except sender (Socket.io)

As far as I know this is how it all works in the python implementation as well. For ease of use I would certainly use it for websockets. The aiohttp seems really powerful but either doesn't have this functionality, buried in the documentation, or written only in the code without any documentation yet.

answered Sep 4, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

While working on VIF in multiple regression I came across this problem

Hello, @Pawan, The as_matrix method is deprecated since 0.23.0, so ...READ MORE

answered Aug 11, 2020 in Python by Dhiman
1,232 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
15,277 views
0 votes
1 answer

Iterating over multiple lists

import itertools for item in itertools.chain(listone, listtwo): #li ...READ MORE

answered Apr 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,459 views
0 votes
1 answer

Multiple line comment in Python

Try this ''' This is a multiline comment. I can ...READ MORE

answered May 31, 2018 in Python by charlie_brown
• 7,720 points
557 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+1 vote
2 answers

Multiple line comment in Python

Try this ''' This is a multiline comment. ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 58,020 points
1,142 views
0 votes
1 answer

Avoiding multiple nested for-loops in python

You can replace the three loops with: from ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,020 points
894 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP