Server load when calling a function: Ajax or WebSockets

I am currently working on a status dashboard in Tornado (python). I need the webpage to update dynamically (by re-rendering the template) whenever the external function is called. I was curious what is the most efficient way to do this? and their any tutorials out there for something like this.

0


source to share


1 answer


If you want the actual push to the server, you will need either server or server related events. Since the events sent by the server are very new (and not supported in so many browsers), the main option for actually pushing to the server is to connect to a web socket.

In webSocket architecture, the client connects to the server and then stays connected. This allows the server to send data to the client over this connection at any time.

Server-dispatched events are of a similar architecture, but with different implementation details for how they are used.




Can't use Ajax to actually push to the server. Ajax is sometimes used to mimic server-dispatched events using what is commonly referred to as long polling. In this case, the client makes an Ajax call, and the server just hangs on the Ajax request for a while, rather than immediately returning a response. If a server-side event arrives at this time, the server can accept a connection awaiting a response and send a response. The client will receive the response, process it, and then make another Ajax call. On long polling, if any server-side action does not happen after a while, the Ajax call will time out and the client must initiate a new connection.

The entire sequence of long polling connections is generally less efficient than a webSocket connection. Thus, if the main problem you are trying to solve is pushing to the server, then a WebSocket connection is probably the most efficient way to do this, which is widely supported.

Here are some similar questions and answers that contain some more discussion: websocket vs rest API for realtime data? and Ajax vs Socket.io .

0


source







All Articles