Smart Software Solutions Inc 108 S Pierre St.
Pierre, SD 57501
605-222-3403
sales@smartsoftwareinc.com

Contact Us

Articles

Realtime Web Data with WebSockets

Published 1 year ago

For years there was no native support for passing data from a web server to a client. The client would always have to initiate an HTTP request to pull data down from the server. This made realtime applications almost impossible. There have been some clever hacks to work around this limitation such as long-polling but there has never been a true way to push unsolicited information from the server until now. Enter WebSockets. WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol has recently been adopted by all major internet browsers and is part of HTML5. Using WebSockets, a web server can push data to it's connected clients in realtime. a WebSocket also allows the client to pass data over the connection to the server making it a bi-directional channel. WebSockets are initiated just like any other HTTP connection but after the connection is established it is "upgraded" to a TCP connection.

As I mentioned earlier, there are other options that serve the same function as WebSockets but they have been less effective and/or less widely implemented.

  1. long-polling: This is more of a hack than a standard. It relies on an http connection's timeout limit. But, unlike WebSockets, the connection must be reset periodically after a timeout occurs, or after a succesful server to client message has occured.
  2. HTTP Streaming: Similar in implementation to long-polling. HTTP streaming takes advantage of the initial HTTP connection established by the client when a request for a web resource is made. Instead of closing this connection when all the data has been sent, the connection is left open. This allows the server to tack on extra data that will be streamed to the client as part of the same request. This data, usually JavaScript is executed immediately by the browser. Unfortunately, like long-polling the browser may eventually trigger a timeout which will close the connection.
  3. Flash XMLSocket relay: This method relies on embedding a Flash application inside the webpage, enough said.
  4. server-sent events (SSE): SSE is another HTML5 specification. While SSEs seems like a winner at first glance, they are lacking support in some of the major browsers at this time. SSEs also only send data in one direction from server to client making them less versatile.


Some examples of where WebSockets can improve your user experience by delivery realtime data are:

  1. Instant Messaging
  2. Online Games
  3. Social Media Feeds
  4. Analytics
  5. Document Editing Collaboration

AUTHOR Preston Scott

Preston has been working for Smart Software since graduating from the University of South Dakota in 2009.  He received a B.S. in Computer Science.  Preston has quickly acquired the skills needed to advance to the Senior and Lead Software Engineer positions.  He has shown a unique ability to quickly adapt to new projects and technologies allowing him to provide robust solutions to complex tasks.  Preston specializes in scripting languages (PHP, ActionScript, JavaScript) as well as low-level language concepts such as memory management, concurrency, and data structures but also has experience and skills to effectively manage high-level languages.  He has also shown the ability to formulate and optimize complex SQL queries.

When not working, he enjoys hunting, fishing, and the many other outdoor activities offered in the beautiful Missouri river valley.