简体   繁体   中英

Send message from server to client after received notification (Tornado+websockets)

I've started learning websockets recently and I've decided to try to learn and use Python's framweork Tornado for creating my simple test project (nothing special, just basic project that could help me learn something about Tornado and websockets in general).

So, this is my idea (workflow):

1)I get http post request from some other app to my server (eg info about some person's name and email)

2) I save received data to my postgresql database and notify listener (publish/subscribe) that the new data has been added to database

3) After receiving the notification server should send message to the client (write_message method)

This is my code far now

simple_server.py

import tornado.httpserver
import tornado.ioloop
import tornado.options 
import tornado.web
import tornado.websocket
import psycopg2
import psycopg2.extensions
import os
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

io_loop = tornado.ioloop.IOLoop.instance()

connection = psycopg2.connect('dbname=mydb user=myusername password=mypassword')
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

class ReceivedDataHandler(tornado.web.RequestHandler):  
    def post(self):     
        cursor = connection.cursor()

        name=self.get_argument('name', 'No name info received')
        email = self.get_argument('email', 'No email info received')
        self.write("New person with name %s and email %s" %(name, email))

        cursor.execute("INSERT INTO mydata VALUES (%s, %s)" %(name, email))
        cursor.execute("NOTIFY test_channel;")

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message('connected!')
    def on_message(self, message):
        self.write_message("Received info about new person: "+message)
    def on_close(self):
        print 'connection closed'

def listen():    
    cursor = connection.cursor()
    cursor.execute("LISTEN test_channel;") 

def receive(fd, events):
    """Receive a notify message from channel I listen."""
    state = connection.poll()
    if state == psycopg2.extensions.POLL_OK:
        if connection.notifies:
            notify = connection.notifies.pop()
            print "New notify message"
io_loop.add_handler(connection.fileno(), receive, io_loop.READ)

if __name__=="__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r'/', IndexHandler),
            (r'/person-info', ReceivedDataHandler),
            (r'/websocket', EchoHandler)
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    listen()
    io_loop.start()

When I test sending post request (via Postman REST Client) to defined url it all works fine. The data is really saved to my db and it really notifies listener that there is new notification but I just don't know how to send that message to client after that. If I could do that then it would display that message in browser and that's all I want to do for this time.

So, all I want to do is actually call write_message function after I receive notification about new entry in database (instead of just printing "new notify message") but I just don't know how to do it in Tornado. I believe it should be very easy actually but as I obviously have lack of experience with Tornado (and asynchronous programming) I'm little stuck.

Thanks for your help

Eventually I found the solution for this problem. I've just added global variable where I add all connected clients and then I send message to each connected client when I receive notification. (it's ok for me beacuse I actually wanted to send message to all connected clients)

SO here is how it looks now

simple_server.py

import tornado.httpserver
import tornado.ioloop
import tornado.options 
import tornado.web
import tornado.websocket
import psycopg2
import psycopg2.extensions
import os
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

io_loop = tornado.ioloop.IOLoop.instance()

connection = psycopg2.connect('dbname=mydb user=myusername password=mypassword')
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

# This is a global variable to store all connected clients
websockets = []

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

class ReceivedDataHandler(tornado.web.RequestHandler):  
    def post(self):     
        cursor = connection.cursor()

        name=self.get_argument('name', 'No name info received')
        email = self.get_argument('email', 'No email info received')
        self.write("New person with name %s and email %s" %(name, email))

        cursor.execute("INSERT INTO mydata VALUES (%s, %s)" %(name, email))
        cursor.execute("NOTIFY test_channel;")

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        self.write_message('connected!')
    def on_message(self, message):
        self.write_message("Received info about new person: "+message)
    def on_close(self):
        print 'connection closed'

def listen():    
    cursor = connection.cursor()
    cursor.execute("LISTEN test_channel;") 

def receive(fd, events):
    """Receive a notify message from channel I listen."""
    state = connection.poll()
    if state == psycopg2.extensions.POLL_OK:
        if connection.notifies:
            notify = connection.notifies.pop()
            for ws in websockets:
                   ws.write_message("my message")
io_loop.add_handler(connection.fileno(), receive, io_loop.WRITE)

if __name__=="__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r'/', IndexHandler),
            (r'/person-info', ReceivedDataHandler),
            (r'/websocket', EchoHandler)
        ],
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        debug=True
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    listen()
    io_loop.start()

Could add this to your EchoHandler.open() method:

io_loop.add_handler(connection.fileno(), self.receive, io_loop.READ)

Remove the receive function and the other io_loop.add_handler call, then write a new version of the receive function, but make it an instance method of the EchoHandler class. That way you can call your write_message method from the new receive method.

Something like this (this is untested, but hopefully gets my point across):

class EchoHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        io_loop.add_handler(connection.fileno(), self.receive, io_loop.READ)
        self.write_message('connected!')

    def on_message(self, message):
        self.write_message("Received info about new person: "+message)

    def on_close(self):
        print 'connection closed'

    def receive(self, fd, events):
        """Receive a notify message from channel I listen."""
        state = connection.poll()
        if state == psycopg2.extensions.POLL_OK:
            if connection.notifies:
                notify = connection.notifies.pop()
                #print "New notify message"
                self.write_message("Your message here")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM