For developing web application in the browser, developers use the technologies like Ajax and Comet. Both technologies need two way communication/rapid update for there operations. This can amount to an “abuse of HTTP”
As a result we end up with the problems like
- Too many connections.
- Too much overhead, when we try to load frequent updates.
- Too great a burden on the clients waiting for all connections with XML and HTTP request. It’s very complicated.
The usual suspect in web sockets are:
- Trading and stock marketing application where live data is needs.
- Multiplayer gaming
- Chat application
- Collaboration on video suffering
The goal of web socket to overcome all the existing problems.
The Web Socket is a set of multiple standards: The Web Socket API is defined by the W3C, and the Web Socket protocol (RFC 6455) and its extensions are defined by the HyBi Working Group (IETF). They provided a mechanism for browser based applications, that two way communication with server that does not read on multiple http connections, instead it is a separate implementation on top of TCP.
The Web Socket technology promises to fill a niche in web applications by enabling browser and server to exchange messages with high frequency, low latency and low bandwidth requirement in full duplex mode. The Web Socket API is almost standardized by the W3C and the JSR-356 will deliver a Java API in JAVA EE7. There are already implementation in node.js and in a number of Servlet containers and java frameworks.
You might have never seen a URL that start with a WS in your browser’s location bar, but behind the scene many of your favourite applications like slack, Gmail, Twitter are using web sockets.
==========
URL
HTTP has its own set of schema such as http and https. Web socket protocol also has similar schema defined in its URL pattern.
The following image shows the Web Socket URL in tokens.
Browser Support
Web Socket protocol is defined as RFC 6455 – a proposed standard. RFC 6455 is supported by various browsers like Internet Explorer, Mozilla Firefox, Google Chrome, Safari, and Opera.
Let us check how to write a application code,
Functions of Web Sockets
Websockets are used for send data between webserver and client.
Steps involved in websocket establishing:
The client setting up an association with server through a procedure known as Websocket handshake.The process starts with the client sending a HTTP ask for to the server. An Upgrade header is asked. In this demand, it educates the server that demand is for Web Socket association. Web Socket URLs are utilized the WS shema.
They are additionally utilized for secure Web Socket associations, which are the identical to HTTPs.
A simple example of initial request headers is as follows −
GET ws://websocket.example.com/ HTTP/1.1
Origin: http://example.com
Connection: Upgrade
Host: websocket.example.com
Upgrade: websocket
Web Sockets involve a key part in the web as well as in the portable business. The importance of Web Sockets are given below..
Websockets as the name shows, are related with the web. Web technologies involved bundle of techniques in some browsers. It is a broad commucation platform which is flood over mutikpe platforms like desktop PCs, portable workstations, tablets and PDAs.
The app that uses websockets technology which I build on html5 platform works on any browser which html5 is enabled.
Web socket is supported in the mainstream operating systems. Every single key player in the versatile business give Websocket APIs in possess local applications.
Chatrooms uses websockets it to a great extent since full duplex mode connection is supported. Update between the client can be transferred simultaneous with much better is.
Web Sockets, a part of the HTML5, allow full duplex communication between web pages and a remote host.
There is also one more advantage of using this fulduples through a connection, it can reduce unnecessary traffic and latency.
Streaming through proxies and firewall are examples.
Web Sockets – Events
Websocket API events are mentioned as follows.
Open
Message
Close
Error
The events are handled with onopen, onmessage, onclose and onerror functions. It can also setup with the addEventListener technique.
Open
It is an initial handshake event between the client and server. if the connection is established between the client and server, the open event is fired from websocket it is onopen.
server.Start(socket) ⇒ {
socket.OnOpen = () ⇒ {
// Add the incoming connection to our list.
clients.Add(socket);
}
// Handle the other events here…
});
Message
The next is message event it happens only after the open event is fired. It inclued server sending data like plain text, binary data and images, on message function is fired afterwards
socket.OnMessage = () ⇒ {
// Display the message on the console.
Console.WriteLine(message);
};
Close
Communication between the client and server is terminated marking the end of communication, onclose event is fired. Nothing can be send and received after the on closed is fired. It can happended due to poor network connectivity.
socket.OnClose = () ⇒ {
// Remove the disconnected client from the list.
clients.Remove(socket);
};
Error
When some kind of mistake or error happened during the communication, onerror event is fired. It is always due to the termination of connection.
socket.OnMessage = () ⇒ {
foreach (var client in clients) {
// Send the message to everyone!
// Also, send the client connection’s unique identifier in order
// to recognize who is who.
client.Send(client.ConnectionInfo.Id + ” says: ” + message);
}
};
Web Sockets – Actions
Events are usually triggered when something happens. On the other hand, actions are taken when a user wants something to happen. Actions are made by explicit calls using functions by users.
The Web Socket protocol supports two main actions, namely −
send( )
close( )
send ( )
This action is preferred for some communication with the server, which includes sending messages, text files, binary data and images.
A chat message, which is sent with the help of send() action, is as follows −
// get text view and button for submitting the message
var textsend = document.getElementById(“text-view”);
var submitMsg = document.getElementById(“tsend-button”);
//Handling the click event
submitMsg.onclick = function ( ) {
// Send the data
socket.send( textsend.value);
}
//Sending the messages is only possible if the connection is open.
close ( )
This method stands for goodbye handshake. It terminates the connection completely and no data can be transferred until the connection is re-established.
var textsend = document.getElementById(“text-view”);
var buttonStop = document.getElementById(“stop-button”);
//Handling the click event
buttonStop.onclick = function ( ) {
// Close the connection if open
if (socket.readyState === WebSocket.OPEN){
socket.close( );
}
}
It is also possible to close the connection deliberately with the help of following code snippet −
socket.close(1000,”Deliberate Connection”);
Performance Checklist
In order to get high performance from websocket service connection between client and server need to be configured such that TLS is not used.
- Use secure WebSocket WSS over TLS for reliable deployments.
- Pay close attention to polyfill performance (if necessary).
- Leverage subprotocol negotiation to determine the application protocol.
- Optimize binary payloads to minimize transfer size.
- Compressing methods like UTF-8 content to minimize transfer size.
- Set the right binary type for received binary payloads.
- Monitor the amount of buffered data on the client.
- Split large application messages to avoid head-of-line blocking.
- Leverage other transports where applicable.
ServerAdminz provides Outsourced 24/7 Technical Support, Remote Server Administration, Server Security, Linux Server Management, Windows Server Management and Helpdesk Management to Datacenters, Hosting companies and ISPs around the world. We specialize in Extended Server Security, Server Hardening, Support of Linux/UNIX/Windows servers, products and services.If you are looking for a server management service provider, you can contact us on sales@serveradminz.com or +1 (845) 271 7172.
ServerAdminz is a server support company specialized in Outsourced 24/7 Web Hosting Support, Remote Infrastructure Management, NOC, Cloud and Enterprise Security Services. With over 10+ of years of experience in working with major Data Centers and ISPs with 130+ experienced technicians, we continue to manage more than 49,000 servers from 85+ countries and has bagged 5 international awards.
Leave A Comment
You must be logged in to post a comment.