Question

I currently have a site setup using Django. I have added Gevent Socketio to add a chat function. I have a need to scale it as there are quite a few users already on the site and can't find a way to do so.

I tried https://github.com/abourget/gevent-socketio/tree/master/examples/django_chat/chat

I am using Gunicorn & the socketio.sgunicorn.GeventSocketIOWorker worker class so at first I thought of increasing the worker count. Unfortunately this seems to fail intermittently. I have started rewriting it to use redis from a few sources I found and have 1 worker on each server which is now being load balanced. However this seems to have the same problem. I am wondering if there is some issue in the gevent socketio code itself which does not allow it to scale.

Here is how I have started which is just the submit message code.

def redis_client():
    """Get a redis client."""
    return Redis(settings.REDIS_HOST, settings.REDIS_PORT, settings.REDIS_DB)

class PubSub(object):
    """
    Very simple Pub/Sub pattern wrapper
    using simplified Redis Pub/Sub functionality.

    Usage (publisher)::

        import redis

        r = redis.Redis()

        q = PubSub(r, "channel")
        q.publish("test data")


    Usage (listener)::

        import redis

        r = redis.Redis()
        q = PubSub(r, "channel")

        def handler(data):
            print "Data received: %r" % data

        q.subscribe(handler)

    """
    def __init__(self, redis, channel="default"):
        self.redis = redis
        self.channel = channel

    def publish(self, data):
        self.redis.publish(self.channel, simplejson.dumps(data))

    def subscribe(self, handler):
        redis = self.redis.pubsub()
        redis.subscribe(self.channel)

        for data_raw in redis.listen():
            if data_raw['type'] != "message":
                continue

            data = simplejson.loads(data_raw["data"])
            handler(data)


from socketio.namespace import BaseNamespace
from socketio.sdjango import namespace
from supremo.utils import redis_client, PubSub
from gevent import Greenlet

@namespace('/chat')
class ChatNamespace(BaseNamespace):
    nicknames = []
    r = redis_client()
    q = PubSub(r, "channel")

    def initialize(self):
        # Setup redis listener
        def handler(data):
            self.emit('receive_message',data)

        greenlet = Greenlet.spawn(self.q.subscribe, handler)

    def on_submit_message(self,msg):
        self.q.publish(msg)
Was it helpful?

Solution

I used parts of code from https://github.com/fcurella/django-push-demo and gevent-socketio 0.3.5rc1 instead of rc2 and it is working now with multiple workers and load balancing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top