Getting Started With WebSockets

Getting Started With WebSockets

Jesse Dunlap

Monday, May 19, 2014

As the web application scene grows increasingly more popular, many organizations are finding themselves transitioning from traditional native applications to completely web based software services, or even tailored internal solutions. With this increased utilization of web applications, web technology is evolving faster than ever, and a need for speedy, real-time web applications is emerging.

WebSockets are a primary contender for the future of web application development. By creating a continuous, bi-directional layer of communication, WebSockets make the complexities of most modern web applications disappear. Gone are the days of AJAX polling an API periodically for updates. Now, the server can simply notify the client when something happens, as soon as it happens, and the client can react accordingly.

The WebSocket protocol was standardized by the IETF as in 2011, and the WebSocket API is being standardized by the W3C. Today, every major browser, including their mobile counterparts, have implemented native support for WebSockets. Older browsers can also utilize WebSockets through an Adobe Flash Player shim, provided with most major WebSocket client libraries. Libraries such as Meteor, Socket.io, and SignalR have started taking advantage of WebSockets, blazing a trail to the future of web applications.

WebSocket development is easy—really easy. A WebSocket client is typically written in JavaScript, and executed by the browser, and a WebSocket server can be implemented in pretty much any language and platform you can think of.

The Client

Let’s start with the WebSocket client. You will hopefully find that like many JavaScript libraries, the WebSocket API is pretty straightforward and easy to understand, especially in comparison to traditional socket interactions.

As you can probably tell, the WebSocket API is very simplistic, making it very pleasant to use. There are a few things you should keep in mind, though:

You should always match WebSocket security with HTTP security. That is to say that if your website is using HTTPS, you should use secure WebSockets. If your website is using secure WebSockets, then you should use HTTPS. Some browsers—such as Firefox—mandate this, and won’t let you establish a WebSocket connection unless security matches up.

It is not necessary to call any sort of connect function on the WebSocket instance. The connection is established automatically. However, issues can arise when trying to send a message to the server in the onconnect event, since depending on your WebSocket server library, the onconnect event may be fired before the server is ready to accept your messages. It’s best to have the server send the first message, and then have the client respond accordingly.

When writing your WebSocket client, you may also want to consider using one of the many WebSocket libraries available. A WebSocket library will give you more advanced WebSocket communication features, and handle some of the more advanced WebSocket features gracefully. Socket.io is a great library if you’re using a Node.js based server, and Microsoft’s SignalR offers an end-to-end solution for WebSocket communication in the ASP.NET world.

The Server

You get a bit more choice when writing your WebSocket server. This is also where things get more platform-specific. Ultimately, though, all servers will boil down to accepting multiple WebSocket connections, and sending/receiving messages with those clients. The example below is written in C# using the Alchemy library for .NET.

 

As mentioned, most WebSocket servers will work roughly the same way as the example above. You’ll also find that most WebSocket server-side libraries provide both a WebSocket server and a WebSocket client. The client can be used to connect to other WebSocket servers if for some reason you want to use the WebSocket protocol outside of a browser. Here are a few libraries for different platforms.

Wrapping Up

WebSockets open up some very exciting possibilities with web application development. Some implementations rely exclusively on WebSockets to provide a lightning fast, single page application, while other implementations simply augment a traditional HTTP web application with some fancy WebSocket support. Whatever you choose to do, you’ll find a very powerful and fun to use API, combined with excellent support across a variety of platforms.

Going forward, the Mozilla Developer Network provides excellent documentation on the WebSocket protocol. Additionally, you may want to take a closer look into Socket.io, since it provides a comprehensive and easy starting point for WebSocket development. Finally, StackOverflow is a great resource for learning more about WebSockets—particularly the WebSocket tag.

Spread the love

Receive occasional updates from us. Don't worry, we don't spam people.