I am writing a small project on a bunch of flask + tornado. General functionality has been written on flask (currently authorization + user registration, a model has also been created), I am trying to write a chat on a tornado.

How to tornado teach to understand under what user we are now authorized, and on his behalf to write chat messages?

PS If anyone wants to participate in the development of this project - you are welcome. I want to do something for voice communication on the web. The goal is to gain experience in this kind of development.

    2 answers 2

    Very interesting and relevant issue for Tornado. There are a lot of ways to implement it, a tornado is so good that out of the box there is no imposed solution, you can give free rein to your imagination and sharpen everything as tasty as possible for your project.

    I propose such an option (I omit the extra code to show only the essence):

    class BaseHandler(tornado.web.RequestHandler): ... def get_current_user(self): user = None user_json = self.get_secure_cookie("mysite_cookie_user") if not user_json: return None logging.info("User ID:%s is get response", str(user_json)) try: user = tornado.escape.json_decode(user_json) except Exception: logging.info("Error get user cookie") return user def get_visiter_settings(self): settings_json = self.get_secure_cookie("mysite_cookie_settings") try: cur_settings = tornado.escape.json_decode(settings_json) logging.info("Visiter settings:%s is get response from cookie.", str(cur_settings)) except Exception: logging.info("Error get settings from cookie.") logging.info("Init new cookie settings.") cur_settings = { "visitor_uuid": str(uuid.uuid4()), "option1": "foo", ... "option10": "bar"} self.set_secure_cookie("mysite_cookie_settings", json.dumps(cur_settings)) logging.info("New cookie settings initialize.") finally: return cur_settings class MainHandler(BaseHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self,*args, **kwargs): current_user = self.get_current_user() visiter_settings = self.get_visiter_settings() 

    Session hashes can be stored for example in Redis.

    In handlers, session status can be received and updated using keys generated by current_user and visiter_settings ["visitor_uuid"].

    For example, for guests of a session to determine by visitor_uuid, for those authorized by current_user +, they can be linked to their existing session by visitor_uuid.

      You can, for example, cookies

      If you have a common database, it is logical to transfer the login to secure_cookies . This principle is based on tornado authentication tornado of the box.

      I will not say about the flag, I have not come across it alas, but I think there is something similar there or, in extreme cases, you can write it yourself.