Educar para criticar

Pra quem sempre lutou pela liberdade não é difícil entender o poder que emana do senso crítico. Afinal, foram necessários 322 anos para nos ver libertos dos Tugas e mais 66 anos para a abolição dos…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Power of WebSocket

In the dark days before WebSocket web developers had to use inefficient techniques to ensure their websites were dynamic. The two most common issues we encountered were large overhead and compatibility issues.

HTTP Polling is one of the most basic ways that web developers (myself included) have used to make realtime websites. In simple terms, HTTP Polling is having your website message the server at set intervals to see if any data needs to be updated. However, this can cause an issue if the interval is set to too small a number, where the server will be overloaded by the massive overhead you are creating. Alternatively, if you set the interval too high you will display stale data for longer periods of time.

The feedback loop of HTTP Polling

Developers quickly realized that HTTP Polling was a messy solution and set out to develop better solutions, starting with LiveConnect, a Java applet that created a connection to the server that remained open for longer periods of time. As a Java applet, it brought with its inconsistencies and compatibility errors when trying to use it across multiple browsers. Developers quickly moved from LiveConnect to Forever Frame, AJAX, then HTTP Long-Polling.

All of these solutions were attempting to leverage the client to perform functions that it wasn’t built for. Most developers considered them to be hacked. On top of that, many issues came up with each of these solutions. The biggest one was back within the day, browsers limited the second connection that came from the same domain. All these solutions required two connections: one for sending and receiving data, and the other for publishing events like logging in. Nowadays, browsers can support many same domain connections, so it is no longer an issue. The second big issue developers ran into was a lack of cross-domain support, meaning that scripts from separate domains couldn’t communicate with each other. The early solution to this was that websites would self-host all their resources, which is a very big restriction and would prevent you from using popular services like the cloud. Access-Control-Allow-Origin header was developed as a response to this, and it allowed for Cross-Origin Resource Sharing, but there were still a variety of compatibility issues making real-time browser solutions an unsustainable concept.

Instead of trying to manage with what they had, developers continued to innovate and WebSocket emerged. In order to become a standard, new technology typically meets two criteria: it functions well, and is simple enough to use that developers are happy to change over to it. WebSocket met both criteria, and with that, it’s adoption grew at an exponential rate. This resulted in higher accessibility when making real-time websites.

WebSocket is a communications protocol, that allows for bi-directional communication between the client and the server. The beautiful part is that it has very little overhead and updates are sent out almost instantaneously. This makes it so you don’t have to worry about overwhelming your server, and it eliminates most stale data issues. You can set up your WebSocket very easily with one line of code.

Once your connection is set up you can listen for pings from the server with the built it onmessage function.

In the above example, we are simply console logging the incoming message from the server, but we could also easily use jQuery to update the DOM to refresh the data on the browser.

Demonstrating the handshake to open a WebSocket

Almost every popular realtime website is using WebSocket now. Twitter, Facebook, and even everyone's favorite website StackOverflow. From instant messaging to pushing notifications, these sites utilize the power of WebSocket.

How Twitter utilizes WebSocket

WebSocket is a very elegant solution to making realtime websites. It’s sheer simplicity and ease of use make me question if we will ever see a better solution. With low overhead and most compatibility issues being phased out, I foresee WebSocket being the go-to for realtime websites for a long time.

If you are interested in further readings or just a different perspective, here are the sources I pulled from outside of my personal experience.

Add a comment

Related posts:

Become a Real World Pilot with Microsoft Flight Simulator 2020

Arriving August 18th this year, Microsoft Flight Simulator 2020 will be the most advanced flight sim in terms of graphics and functionality to be ever made. The map for the Flight sim is literally…

Diabetes Prediction application using machine learning algorithms.

In this article we are going to build an application which is going to predict whether the user has chances of being diabeteic based on a little information provided by the user. We are going to…

PSU Banks on a Buying spree as Bank Nifty hits at new High!!!

As expected PSU Banks ruled the roost at today's trading even as Bank Nifty made another new high up by 227.70 points to close the day @ 38521.5. The day sees the indices moved in a narrow range with…