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
• 14,970 points
49 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 56,120 points

Related Questions In Python

+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,700 points
525 views
0 votes
1 answer

Iterating over multiple lists

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

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

Multiple line comment in Python

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

answered May 30, 2018 in Python by charlie_brown
• 7,710 points
20 views
0 votes
1 answer

Multiple line comment in python

No, you can simply use triple codes ...READ MORE

answered Jun 27, 2018 in Python by v.liyyah
• 1,290 points
19 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
25 views
+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
• 56,120 points
32 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
• 56,120 points
33 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.