How to show continuous real time updates like facebook ticker, meetup.com home page does?

后端 未结 4 986
抹茶落季
抹茶落季 2020-12-14 12:26

How to show continuous real time updates in browser like facebook ticker, meetup.com home page does? In python, PHP, node.js and what would be the performance impact at the

4条回答
  •  执笔经年
    2020-12-14 13:12

    I would suggest implementing a socket like connection using SockJS or Socket.io as the client side JavaScript library and then using Tornado on the server side to publish any state changes to the client. The code is fairly simple.

    The client side code depends on which library you choose. SockJS or SocketIO. Or if you just want to use Websockets directly it's very simple:

    update_socket = new WebSocket("ws://my_server.com/listening_url");
    update_socket.onmessage = function (evt) {
        $("#my_div").html(evt);
    };
    

    The server side code is also pretty simple:

    import tornado
    
    class UpdateHandler(tornado.websocket.WebSocketHandler):
    
        def open(self):
            self.write_message('Hi client')
            # listen for some events that are occurring
            for message in function_that_generates_events():
                self.write(message) 
    
        def on_message(self, message):
            # Do something with incoming messages
    
        def on_close(self):
            # tidy up
    
    app = tornado.web.Application(('r/listening_url',UpdateHandler))
    app.listen(9000)
    

提交回复
热议问题