WebSockets – Discussion

Discuss WebSockets ”; Previous Next Web sockets are defined as a two-way communication between the servers and the clients, which mean both the parties, communicate and exchange data at the same time. This protocol defines a full duplex communication from the ground up. Web sockets take a step forward in bringing desktop rich functionalities to the web browsers. It represents an evolution, which was awaited for a long time in client/server web technology. Print Page Previous Next Advertisements ”;

WebSockets – Mobile APP

WebSockets – Mobile APP ”; Previous Next WebSocket, as the name implies, is something that uses the web. The web is usually interwoven with browser pages because that are the primary means of displaying data online. However, non-browser programs too, use online data transmission. The release of the iPhone (initially) and the iPad (later) introduced a brand new world of web interconnectivity without necessarily using a web browser. Instead, the new smartphone and tablet devices utilized the power of native apps to offer a unique user experience. Why Mobile Matters? Currently, there are one billion active smartphones out there. That is, millions of potential customers for your applications. These people use their mobile phone to accomplish daily tasks, surf the internet, communicate, or shop. Smartphones have become synonymous to apps. Nowadays, there is an app for any usage, a user can think of. Most of the apps connect to the internet in order to retrieve data, make transactions, gather news, and so on. It would be great to use the existing WebSocket knowledge and develop a WebSocket client running natively on a smartphone or tablet device. Native Mobile App Vs Mobile Website Well, this is a common conflict and as usual, the answer depends on the needs of the target audience. If a user is familiar with the modern design trends, designing a website that is responsive and mobile friendly is now a must. However, the end user must be sure that the content, which is what really matters, is equally accessible via a smartphone, as it is via a classic desktop browser. Definitely, a WebSocket web app will run on any HTML5-compliant browser, including mobile browsers such as Safari for iOS and Chrome for mobile. Therefore, there are no worries about compatibility issues with smartphones. Prerequisites In order to develop a smartphone app, installation of development tools and SDKs are required. WebSockets can act as a universal hub for transmitting messages between connected mobile and tablet clients. We can implement a native iOS application, which communicates with a WebSocket server just like the HTML5 JavaScript client. Print Page Previous Next Advertisements ”;

WebSocket – API

WebSockets – API ”; Previous Next API – Definition API, an abbreviation of Application Program Interface, is a set of routines, protocols, and tools for building software applications. Some important features are − The API specifies how software components should interact and APIs should be used when programming graphical user interface (GUI) components. A good API makes it easier to develop a program by providing all the building blocks. REST, which typically runs over HTTP is often used in mobile applications, social websites, mashup tools, and automated business processes. The REST style emphasizes that interactions between the clients and services is enhanced by having a limited number of operations (verbs). Flexibility is provided by assigning resources; their own unique Universal Resource Identifiers (URIs). REST avoids ambiguity because each verb has a specific meaning (GET, POST, PUT and DELETE) Advantages of Web Socket Web Socket solves a few issues with REST, or HTTP in general − Bidirectional HTTP is a unidirectional protocol where the client always initiates a request. The server processes and returns a response, and then the client consumes it. Web Socket is a bi-directional protocol where there are no predefined message patterns such as request/response. Either the client or the server can send a message to the other party. Full Duplex HTTP allows the request message to go from the client to the server and then the server sends a response message to the client. At a given time, either the client is talking to the server or the server is talking to the client. Web Socket allows the client and the server to talk independent of each other. Single TCP Connection Typically, a new TCP connection is initiated for an HTTP request and terminated after the response is received. A new TCP connection needs to be established for another HTTP request/response. For Web Socket, the HTTP connection is upgraded using standard HTTP upgrade mechanism and the client and the server communicate over that same TCP connection for the lifecycle of Web Socket connection. The graph given below shows the time (in milliseconds) taken to process N messages for a constant payload size. Here is the raw data that feeds this graph − The graph and the table given above show that the REST overhead increases with the number of messages. This is true because that many TCP connections need to be initiated and terminated and that many HTTP headers need to be sent and received. The last column particularly shows the multiplication factor for the amount of time to fulfil a REST request. The second graph shows the time taken to process a fixed number of messages by varying the payload size. Here is the raw data that feeds this graph − This graph shows that the incremental cost of processing the request/response for a REST endpoint is minimal and most of the time is spent in connection initiation/termination and honoring HTTP semantics. Conclusion Web Socket is a low-level protocol. Everything, including a simple request/response design pattern, how to create/update/delete resources need, status codes etc. to be builds on top of it. All of these are well defined for HTTP. Web Socket is a stateful protocol whereas HTTP is a stateless protocol. Web Socket connections can scale vertically on a single server whereas HTTP can scale horizontally. There are some proprietary solutions for Web Socket horizontal scaling, but they are not based on standards. HTTP comes with a lot of other goodies such as caching, routing, and multiplexing. All of these need to be defined on top of Web Socket. Print Page Previous Next Advertisements ”;

WebSockets – Functionalities

WebSockets – Functionalities ”; Previous Next Web Socket represents a major upgrade in the history of web communications. Before its existence, all communication between the web clients and the servers relied only on HTTP. Web Socket helps in dynamic flow of the connections that are persistent full duplex. Full duplex refers to the communication from both the ends with considerable fast speed. It is termed as a game changer because of its efficiency of overcoming all the drawbacks of existing protocols. Web Socket for Developers and Architects Importance of Web Socket for developers and architects − Web Socket is an independent TCP-based protocol, but it is designed to support any other protocol that would traditionally run only on top of a pure TCP connection. Web Socket is a transport layer on top of which any other protocol can run. The Web Socket API supports the ability to define sub-protocols: protocol libraries that can interpret specific protocols. Examples of such protocols include XMPP, STOMP, and AMQP. The developers no longer have to think in terms of the HTTP request-response paradigm. The only requirement on the browser-side is to run a JavaScript library that can interpret the Web Socket handshake, establish and maintain a Web Socket connection. On the server side, the industry standard is to use existing protocol libraries that run on top of TCP and leverage a Web Socket Gateway. The following diagram describes the functionalities of Web Sockets − Web Socket connections are initiated via HTTP; HTTP servers typically interpret Web Socket handshakes as an Upgrade request. Web Sockets can both be a complementary add-on to an existing HTTP environment and can provide the required infrastructure to add web functionality. It relies on more advanced, full duplex protocols that allow data to flow in both directions between client and server. Functions of Web Sockets Web Sockets provide a connection between the web server and a client such that both the parties can start sending the data. The steps for establishing the connection of Web Socket are as follows − The client establishes a connection through a process known as Web Socket handshake. The process begins with the client sending a regular HTTP request to the server. An Upgrade header is requested. In this request, it informs the server that request is for Web Socket connection. Web Socket URLs use the ws scheme. They are also used for secure Web Socket connections, which are the equivalent 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 Print Page Previous Next Advertisements ”;

WebSockets – Closing a Connection

WebSockets – Closing a Connection ”; Previous Next Close event marks the end of a communication between the server and the client. Closing a connection is possible with the help of onclose event. After marking the end of communication with the help of onclose event, no messages can be further transferred between the server and the client. Closing the event can occur due to poor connectivity as well. The close() method stands for goodbye handshake. It terminates the connection and no data can be exchanged unless the connection opens again. Similar to the previous example, we call the close() method when the user clicks on the second button. var textView = document.getElementById(“text-view”); var buttonStop = document.getElementById(“stop-button”); buttonStop.onclick = function() { // Close the connection, if open. if (socket.readyState === WebSocket.OPEN) { socket.close(); } } It is also possible to pass the code and reason parameters we mentioned earlier as shown below. socket.close(1000, “Deliberate disconnection”); The following code gives a complete overview of how to close or disconnect a Web Socket connection − <!DOCTYPE html> <html> <meta charset = “utf-8” /> <title>WebSocket Test</title> <script language = “javascript” type = “text/javascript”> var wsUri = “ws://echo.websocket.org/”; var output; function init() { output = document.getElementById(“output”); testWebSocket(); } function testWebSocket() { websocket = new WebSocket(wsUri); websocket.onopen = function(evt) { onOpen(evt) }; websocket.onclose = function(evt) { onClose(evt) }; websocket.onmessage = function(evt) { onMessage(evt) }; websocket.onerror = function(evt) { onError(evt) }; } function onOpen(evt) { writeToScreen(“CONNECTED”); doSend(“WebSocket rocks”); } function onClose(evt) { writeToScreen(“DISCONNECTED”); } function onMessage(evt) { writeToScreen(”<span style = “color: blue;”>RESPONSE: ” + evt.data+”</span>”); websocket.close(); } function onError(evt) { writeToScreen(”<span style = “color: red;”>ERROR:</span> ” + evt.data); } function doSend(message) { writeToScreen(“SENT: ” + message); websocket.send(message); } function writeToScreen(message) { var pre = document.createElement(“p”); pre.style.wordWrap = “break-word”; pre.innerHTML = message; output.appendChild(pre); } window.addEventListener(“load”, init, false); </script> <h2>WebSocket Test</h2> <div id = “output”></div> </html> The output is as follows − Print Page Previous Next Advertisements ”;

WebSockets – Overview

WebSockets – Overview ”; Previous Next In literal terms, handshaking can be defined as gripping and shaking of right hands by two individuals, as to symbolize greeting, congratulations, agreement or farewell. In computer science, handshaking is a process that ensures the server is in sync with its clients. Handshaking is the basic concept of Web Socket protocol. The following diagram shows the server handshake with various clients − Web Sockets – Definition Web sockets are defined as a two-way communication between the servers and the clients, which mean both the parties communicate and exchange data at the same time. The key points of Web Sockets are true concurrency and optimization of performance, resulting in more responsive and rich web applications. Description of Web Socket Protocol This protocol defines a full duplex communication from the ground up. Web sockets take a step forward in bringing desktop rich functionalities to the web browsers. It represents an evolution, which was awaited for a long time in client/server web technology. The main features of web sockets are as follows − Web socket protocol is being standardized, which means real time communication between web servers and clients is possible with the help of this protocol. Web sockets are transforming to cross platform standard for real time communication between a client and the server. This standard enables new kind of the applications. Businesses for real time web application can speed up with the help of this technology. The biggest advantage of Web Socket is it provides a two-way communication (full duplex) over a single TCP connection. URL HTTP has its own set of schemas 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 The latest specification of 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. Print Page Previous Next Advertisements ”;