How can I create multiple chats in Tornado?

I am trying to create a Tornado application with several chats. Chats should be based on the HTML5 website. Web sites communicate nicely, but I always run into the problem that every message is sent twice.

The application uses four classes to handle chat:

  • Chatcontains all written messages so far and a list with all waitersthat should be notified

  • ChatPoolserves to search for new websites - it creates a new chat when there is no one with the required one scratch_idor returns an existing chat instance.

  • ScratchHandler - This is the entry point for all HTTP requests - it analyzes the basic template and returns all the details of the client side.

  • ScratchWebSocket queries the database for user information, sets up a connection, and notifies the chat instance if a new message is to be distributed.

How to prevent posts from being posted multiple times? How can I create an application for several chats with a tornado?

import uuid
import tornado.websocket
import tornado.web
import tornado.template

from site import models
from site.handler import auth_handler

class ChatPool(object):

    # contains all chats

    chats = {}

    @classmethod
    def get_or_create(cls, scratch_id):

        if scratch_id in cls.chats:
            return cls.chats[scratch_id]
        else:
            chat = Chat(scratch_id)
            cls.chats[scratch_id] = chat
            return chat


    @classmethod
    def remove_chat(cls, chat_id):

        if chat_id not in cls.chats: return
        del(cls.chats[chat_id])


class Chat(object):

    def __init__(self, scratch_id):

        self.scratch_id = scratch_id
        self.messages = []
        self.waiters = []

    def add_websocket(self, websocket):
        self.waiters.append(websocket)

    def send_updates(self, messages, sending_websocket):
        print "WAITERS", self.waiters   
        for waiter in self.waiters:
            waiter.write_message(messages)
        self.messages.append(messages)


class ScratchHandler(auth_handler.BaseHandler):

    @tornado.web.authenticated
    def get(self, scratch_id):

        chat = ChatPool.get_or_create(scratch_id)
        return self.render('scratch.html', messages=chat.messages,
                                           scratch_id=scratch_id)


class ScratchWebSocket(tornado.websocket.WebSocketHandler):

    def allow_draft76(self):
        # for iOS 5.0 Safari
        return True

    def open(self, scratch_id):

        self.scratch_id = scratch_id
        scratch = models.Scratch.objects.get(scratch_id=scratch_id)

        if not scratch:
            self.set_status(404)
            return

        self.scratch_id = scratch.scratch_id

        self.title = scratch.title
        self.description = scratch.description
        self.user = scratch.user

        self.chat = ChatPool.get_or_create(scratch_id)
        self.chat.add_websocket(self)        

    def on_close(self):
        # this is buggy - only remove the websocket from the chat.
        ChatPool.remove_chat(self.scratch_id)

    def on_message(self, message):
        print 'I got a message'
        parsed = tornado.escape.json_decode(message)
        chat = {
            "id": str(uuid.uuid4()),
            "body": parsed["body"],
            "from": self.user,
            }

        chat["html"] = tornado.escape.to_basestring(self.render_string("chat-message.html", message=chat))
        self.chat.send_updates(chat, self)

NOTE : after feedback from @A. Jesse changed the method send_updatesfrom Chat. Unfortunately, it still returns double values.

class Chat(object):

    def __init__(self, scratch_id):

        self.scratch_id = scratch_id
        self.messages = []
        self.waiters = []

    def add_websocket(self, websocket):
        self.waiters.append(websocket)

    def send_updates(self, messages, sending_websocket):

        for waiter in self.waiters:
            if waiter == sending_websocket:
                continue
            waiter.write_message(messages)

         self.messages.append(messages)

2.EDIT . I compared my code with the example provided by demos. In the websocket example, the new message propagates to waiters through a subclass WebSocketHandlerand class method. In my code, this is done with a shared object:

From the demos:

class ChatSocketHandler(tornado.websocket.WebSocketHandler):

    @classmethod
    def send_updates(cls, chat):
        logging.info("sending message to %d waiters", len(cls.waiters))

        for waiter in cls.waiters:
            try:
                waiter.write_message(chat)
            except:
                logging.error("Error sending message", exc_info=True)

My application using an object and no subclass of WebSocketHandler

class Chat(object):

    def send_updates(self, messages, sending_websocket):

        for waiter in self.waiters:
            if waiter == sending_websocket:
                continue
            waiter.write_message(messages)

        self.messages.append(messages)
+3
source share
2

Tornado, - . , , nginx. , , .

Tornado , . :

Tornado-Redis-Chat

Live Demo

Tornado Redis Pub/Sub - . , , Pub/Sub.

+8

on_message -, -, . : ?

+2

All Articles