HTML5 Web Sockets API
Use Websockets to have a full duplex, bi-directional connection with the server over single connection.
HTML5 WebSocket API
1. WebSocket is a new addition to the HTML5 specification, it allows a web server to establish a connection with the browser and communicate directly without any lag or delay.
2. A regular communication consists of a series of requests and responses between the browser and webserver. This technique is not fit for realtime web application.
3. So with websockets we can establish a connection once and then communications between the webserver and browser can go without any lag or delay(in realtime).
4. This technology is very essential for applications which require regular and quick updates from the webserver. Eg: Stock Market indexes tickers, Multiplayer games, chat applications etc.
Browser Support for HTML5 WebSockets.
1.The HTML5 WebSockets API is supported on latest versions of all major browsers.
Internet Explorer, Apple Safari, Mozilla Firefox, Opera , Google Chrome.NOTE-Internet explorer 9 supports HTML5 Websockets, earlier versions may not.
1.In the normal Request-Response model the communication is carried using the HTTP protocol, while websockets use the TCP/IP protocol.
2.Since, TCP/IP layer is below HTTP layer, hence the overhead involved with TCP/IP protocol is significantly less compared to HTTP.
3.For regular HTTP protocol, the schemes used are http:// or https:// ( for secured), while using websockets we use ws:// or wss:// (for secured) schemes.
NOTE-By using a custom protocol over HTTP the small data packets can be exchanged very quickly between client and server, without any significant lag.
Using HTML5 WebSocket.
1.To initiate a WebSocket connection we create a WebSocket socket object using the WebSocket() Constructor.
var Socket = new WebSocket("ws:// www.example.com/webserver.php");
2. The argument within the WebSocket() constructor is a URL to which the host contects to, and sometimes even the port number.
3. The Protocol used for the connection is ws:// or wss:// (for secured connection)
Example: Creating a Web Socket Connection.
2.After creating a Websocket object, the current state of the connection can be determined using the attribute readyState.
3. readyState attribute can assume four values as shown in table below.
Values for WebSockets attribute readyState.
|CONNECTING||"0"||The Connection is being Established.
|OPEN||"1"||The Connection has been Established.
|CLOSING||"2"||The Connection is beginning to close
|CLOSED||"3"||The Connection is closed.
WebSocket Method: send().
1. After opening a WebSocket Connection we can send data through the connection, using method send().
Send Message with WebSocket method send().
While a Web Socket connection is alive, four types of events occur.
Websocket Events with Event Handlers.
|open||onopen||Fired when the websocket connection has been successfully made.
|close||onclose||Fired when the websocket has been closed.
|error||onerror||Fired when an error occur.
|message||onmessage||Fired when the server sends a message to the client.
WebSocket events and event handlers
Closing the websocket with method : close().
The Connection between the client and server can be closed using the WebSocket Method close().
Close Connection with WebSocket : close()
HTML5 WebSocket Complete Demo
1. In the below demo we assign event handlers for each of the four events that occur in the limetime of a WebSocket Connection.
2. Message is Exchanged between the Client and Server using HTML5 WebSocket API. After which the WebSocket Connection is closed.