WebSockets Communicating with Server - WebSockets

How websockets communicate with server?

Web is mainly built around the request/response paradigm of HTTP. Client loads up a web page and waits until the user clicks onto the next page. Around 2005, AJAX started to make the web feel more dynamic. Still, all HTTP communications are directed by the client, which needs user interaction or periodic polling for loading new data from the server.

Technologies which enable the server to send data to a client in every moment when it knows that new data is available in surroundings. They are called "Push" or “Comet”.

With long polling, client opens an HTTP connection to the server, which keeps it open until it sends a response. Whenever the server has new data, it sends the response. Long polling and the other techniques work reasonably well. However, all these share a common problem that they carry the overhead of HTTP, which does not make them well suited for low latency applications. For example, a multiplayer shooter game in the browser or any other online game with a real-time component.

Bringing Sockets to the Web

Web Socket specification defines an API which establishes "socket" connections between a web browser and a server. In layman terms, there is a determined connection between the client and the server for sending data at any time.

Web socket connection can be simply opened using a constructor

ws is the new URL schema for WebSocket connections. Wss is also used for secure WebSocket connections similar to https which is used for secure HTTP connections.

Attaching few event handlers directly to the connection allows the user to know when the connection is opened, received incoming messages, or if there is an error.

Second argument accepts optional subprotocols. It can be a string or an array of strings. Each string must represent a subprotocol name and server accepts only one of passed subprotocols in the array. Accepted subprotocol can be determined by accessing protocol property of WebSocket object.

Communicating with the Server

When a connection to the server is established (when the open event is fired) data can be sent to the server using send (your message) method on the connection object. It supports only strings, but in the latest specification, it now can send binary messages too. To send binary data, Blob or ArrayBuffer object is used.

Similarly, server might send messages at any time. Whenever this happens, onmessage callback will be fired. Callback receives an event object and the actual message is accessible via data property.

WebSocket can also receive binary messages in the latest spec. Binary frames will be received in Blob or ArrayBuffer format. For specifying the format of the received binary, set binaryType property of WebSocket object to either 'blob' or 'arraybuffer'. Default format is 'blob'.

Another newly added feature of WebSocket is extensions. Using extensions, it is possible to send frames compressed, multiplexed, etc.

Cross-Origin Communication

Cross-origin communication is a modern protocol which is baked right into WebSocket. WebSocket will enable the communication between parties on any domain. Server decides whether to make its service available to all clients or only to those who reside on a set of well-defined domains.

Proxy Servers

WebSocket has compatibility problem with proxy servers, which facilitate HTTP connections in most company networks. WebSocket protocol uses HTTP upgrade system (normally used for HTTP/SSL) to "upgrade" an HTTP connection to a WebSocket connection. Some proxy servers drop the connection as they don’t like this. Therefore, even if a given client uses the WebSocket protocol, it may not be possible to establish a connection.

The Server Side

WebSocket create an entirely new usage pattern for server side applications. While traditional server stacks such as LAMP are designed around the HTTP request/response cycle they don’t deal well with a large number of open WebSocket connections. Keeping a large number of connections open at the same time, requires an architecture which receives high concurrency at a low performance cost.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

WebSockets Topics