How to show continuous real-time updates like facebook ticker, meetup.com homepage?

How to show continuous real-time updates in a browser, for example facebook ticker, meetup.com homepage? In python, php, node.js and what is the server side performance impact? Also, how can we achieve the same update if the page is cached by CDN like akamai?

+7
source share
4 answers

You have two options (others are described in detail above). In case you are not familiar with some of the conceptual ideas behind each option, I decided that I would tell them a couple of lines. Please note that I present these concepts at a very, very high level.

Your three options:

  • Short poll
  • Web socket
  • Comet / long-polling

Short survey

A short survey overcomes the one-way communication between the Client-Server, forcing the client to continuously send requests to the form server:

Client: Do you have a message for me? Server: No. Client: (wait x seconds) Client: Do you have a message for me? Server: No. Client: (wait x seconds) Client: Do you have a message for me? Server: Yes. Here it is! Client: Yay! Client: (update message) 

The constant grunt on behalf of the Client is called Poll . To implement this structure, you need to configure the server to "listen" to these polling requests from the client. The server will also need to store these messages somewhere, so that when the messages are ready, the server can deliver them. At a very high simplified level, your server needs to:

  • Accept shared web requests
  • Accept Survey Requests
  • Running background jobs that retrieve messages
  • Store these messages somewhere so that the server can check them when polling requests arrive.

You also need to associate these polling requests with some sort of session identifier for the user so that the right messages get to the right person. In general, the paradigm is complex and, in my opinion, ineffective.

Web sockets

Web sockets are new to HTML5. The main idea is that the Client can support a direct connection to the server, and they can transfer information to each other back and forth. Therefore, instead of the usual one: clients send a GET request โ†’ The server responds with contents, web sockets allow maintaining a continuous dialogue.

To install this you need to:

  • WebSocket compatible browsers (not all).
  • A server that can handle web sockets (not sure how to state this, but not all servers are configured for this kind of arrangement).

The setup is somewhat complicated, although simpler than a lengthy survey:

  • Client supports connection to a server that supports Web-Socket
  • Server pushes results for client via web socket
  • Customer Updates Page Based on Results

You will see that this template is called Push Notifications (of course, if you have an iPhone that you encountered), since the Server was authorized to click on the โ€œstuffโ€ to the client (how impolite!). Since there are many nuances of the client and server, I would recommend testing something like Pusher , which is basically a web service for handling all the hard parts of web sockets. This will be an easy way to check and play with the template before proceeding with the configuration by yourself. It has both client and server libraries.

We hope that this information will give you a basic level to solve your problem. Other answers have more direct information on how to solve each scenario.

Comet / Long Poll

An alternative, seemingly cross-browser approach to web sockets is Long-Polling (see Comet ). In this case, your client establishes a connection to the server and leaves it hanging, waiting for data to be returned. The setup for this is a bit complicated, but it is the middle line between Short Poll and web sockets .

+18
source

I would suggest implementing a socket, for example, connecting via SockJS or Socket.io as a client-side JavaScript library, and then using Tornado on the server side to publish state changes to the client. The code is pretty simple.

The client code depends on the library you choose. SockJS or SocketIO. Or, if you just want to use Websockets directly, it is very simple:

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

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) 
+5
source

You can use polling, long polling or if you want a push system. The simplest would be a survey. However, all solutions require client-side coding.

Efficiency depends on your decision. The easiest way to implement a survey. A survey with a short frequency really performs a request each, say, 100 ms, simulating in real time. A lengthy survey will have less impact, but over a more or less long time, it will open many queries.

0
source

Jetty

Ajax push engine

Socket.io

These are ways of implementing a comet.

I would recommend Socket.io which is implemented using Node.js

as it uses the best connection method available

0
source

All Articles