Tuesday, June 12, 2012

Html5 Web Sockets Example

Html5 Web Sockets will singular handedly revolutionize web technologies as we know it. The purpose of this post is to account for what techniques have been used to simulate server push until now, define Html5 web sockets, and then give an example of how to use it in your Html5 application.

What is polling?
Until now, the web has been one directional. In other words, web pages could only send a ask to a web server, and not the other way around. Once Ajax came along in 2005, web developers swiftly adopted techniques to simulate a ask from server to client known as polling. There are two types of polling, short polling and long polling.

Sockets & Socket Sets Hand Tools

Short polling is implemented by development a ask to the web server every few seconds or so to see if data has changed. If it has, the web server will respond with the new data. Otherwise, it will respond with a blank message. The drawback to this technique, however, is both a surplus is server requests and an overhead in Cpu usage on the web server to permanently check if an update to the data has been made.

Html5 Web Sockets Example

#1 Sale Now Belkin Conserve Socket F7C009q Energy-Saving Outlet


Belkin Conserve Socket F7C009q Energy-Saving Outlet Feature

  • Easy to set timer; 30 minutes, 3 hours or 6 hours. Continuous Duty Electrical Rating: 120V~/15A/60Hz/1800W. Operating Power Consumption: 0.18W
  • Saves energy; completely cuts power after timer expires
  • Makes your home safer by automatically shutting off space heaters, curling irons, coffee makers etc.
  • Can be used throughout home for multiple devices including fans, cell phone chargers, lights, kitchen appliances, electric toothbrushes etc.
  • Ships in Certified Frustration-Free Packaging

Belkin Conserve Socket F7C009q Energy-Saving Outlet Overview

Belkin Conserve Socket - Energy Saving Outlet (F7C009q).Makes your home safer by automatically shutting off space heaters, curling irons, coffee makers.Can be used throughout home for multiple devices including fans, cell phone chargers, lights, kitchen appliances, electric toothbrushes.

Available at Amazon Check Price Now!

Related Products

Customer Reviews




*** Product Information and Prices Stored: Jun 12, 2012 11:03:47

Long polling is implemented by development a singular ask to the web server and holding the connection open until the data has changed, at which point the web server sends back a response. The drawback to this technique, like short polling, is that the web server still has to check if the data has changed every few seconds or so, creating an overhead in Cpu usage.

What's an Html5 web socket?
This is where Html5 web sockets come in. Html5 will be the first Html specification to preserve client side web sockets. In other words, when data changes on the web server, the web server can send a ask to client, eliminating the need for polling.

Html5 web socket example
Step 1: generate a WebSocket with a valid Url

Create a new WebSocket connection to WebSocket server at finance.example.com.

var stockTickerWebSocket = new WebSocket("ws://finance.example.com");

Note that a ws:// and wss:// prefix indicate a WebSocket and a derive WebSocket connection respectively. The default port for WebSockets is 81 and the default port for derive WebSocket is 815.

Step 2: Attach JavaScript Callback Functions

Associate event listeners to deal with each phase of the connection life cycle.

stockTickerWebSocket.onopen = function(evt)

alert("Stock Ticker connection open...");
;
stockTickerWebSocket.onmessage = function(evt)
alert( "Received Ticker Update: " + evt.data);
;

stockTickerWebSocket.onclose = function(evt)

alert("Connection closed.");
;

Step 3: Send and Receive Data

To send a message to the server, naturally call the postMessage recipe on the webocket with the article you wish to send to the server.

stockTickerWebSocket.postMessage("Buy: Goog,100@200.25″);

This will send the Buy message to the server. Any message advent from the server will be delivered to the onmessage callback registered in step #2.

Step 4: Disconnect When Done

When completed, call the disconnect() recipe to close the WebSocket connection.

stockTickerWebSocket.disconnect();

As demonstrated in the example above, there are no Http requests made to the server from the client side to retrieve data, instead the data was pushed to the client from the server - when it becomes available.

When a new WebSocket connection is established the browser opens an Http connection to the server first and then negotiates with the server to upgrade the connection to a dedicated and persistent WebSocket connection. This process automatically sets up a tunnel through to the server - passing through all network agents (proxies, routers, and firewalls) in the middle (very much like Https establishing a secure, endtoend connection), solving numerous issues that the assorted Comet programming techniques encountered. Once established the WebSocket is a fullduplex channel between the client and the server.

Example provided by http://www.indicthreads.com

Html5 Web Sockets Example

My Links : Band Saws Woodworking Tools home improvement Wrenches Hand Tools Multitools Hand Tools Arc Welding Equipment Power Tools

No comments:

Post a Comment