views:

181

answers:

1

I'm writing a chat feature (like the Faceboook.com one) for a Google App Engine site. I need a way to keep track of what users have new messages. I'm currently trying to use Memcache:

class Message():
    def __init__(self, from_user_key, message_text)
        self.from_user_key = from_user_key
        self.message_text = message_text

class NewMessages():
    def __init__(self):
        self.messages = []
    def add_message(self, message):
        self.messages.append(message)
    def get_messages(self):
        return self.messages
    def messages_sent(self):
        self.messages = [] #Clear all messages

def ChatUserManager():
    def load(user_key):
        manager = memcache.get("chat_user_%s" % user_key)
        if manager is not None:
            return manager
        else:
            manager = ChatUserManager(user_key)
            memcache.set("chat_user_%s" % user_key, manager)
            return manager
    def save(self):
        memcache.set("chat_user_%s" % user_key, self)
    def __init__(self, user_key):
        self.online = True
        self.new_messages = NewMessages()
        self.new_data = False
        self.user_key = user_key
    def recieve_message(self, message):
        self.new_data = True
        self.new_messages.add_message(Message(from_user_key, message_text))
    def send_message(self,  message):
        to_manager = ChatUserManager.load(message.from_user_key)
        to_manager.recieve_message(message)
    def client_receive_success(self):
        self.new_data = False
        self.new_messages.messages_sent()

This chat is user to user, like Facebook or an IM session, not group chat.

Each user will poll a url with ajax to get new messages addressed to them every x seconds. The chat manager will be loaded on that page (ChatUserManager.load(user_key)) and new messages will be checked for. When they are sent the manager will be told that the messages have been sent (manager.client_receive_success()), and then saved back to memcache (manager.save()).

When a user sends a message in the javascript client, it will send an ajax request to a url. The url will load the client's UserChatManager and call .send_message(Message(to_user_key, message_string))).

I'm concerned about the practicality of this model. If everything is in memcache how will it be synchronized across different pages?

Is there a better way to do this?

I do admit that I'm not a python pro yet so the code might not be very pythonic, are there any best practices I'm missing?

+3  A: 

The problem isn't so much about how to share data between "pages" but how will the usability of the service will be impacted by using memcache.

There are no guarantees associated with data persistence in memcache: one moment its there, the other it might not.

jldupont
So would using memcache + datastore fallback (Check memcache; if not there: get from datastore and set memcache key) make this approach work?I realize that memcache is transient but for a chat feature it shouldn't matter too much right? I only need the new message data in memcache at 5min max (I'll put the expiration in there too).
Josh Patton
jldupont