What is Websocket?

Websocket is bi-directional communication protocol over web that helps
client to server and server to client communication on single TCP
connection and on same port.

What is the use of Websocket and how it is helpful? How things works before Websocket ?

For communication on web, HTTP is the standard protocol that is used.

HTTP is stateless protocol. It means,1. Client Request connection2. Connection established3. Client sends a request.4. Server gives response.5. Connection closedNow client and server are independent and unaware of each other.If client want to communicate again then it has to again go with above steps,Also, request is always initiated by client and server can't initiate communication.

So, if Server has to say something to the client then how to achieve that?

First of all, why server want to communicate with the client, Lets see below example,

1. Say, Jayesh requested for fare information of Flight number MH 198 from Mumbai to Ahmedabad.2. Server responded to Jayesh "Price is 5000 INR."3. Jayesh is thinking whether to book a flight at 5000 INR and then finally after 5 minutes, he made a mind to book it.4. Now after filling long form of passenger list and other flight information, when Jayesh pressed the "Book" button, he found that now Flight price is 6000 INR, how this happen??????????5. Jayesh is frustrated and thinking why server not notified him before filling the long form on passenger list.

Ideally, Server should notify Jayesh that flight price is changed, please refresh the page. But how the server will know that to which all clients he need to inform and top of all, Server doesn't know any information on client.

This is possible only if server is capable of identifying client that, "who all client are connected to server and notify them on updates".

As communication happens over HTTP, Before Websocket, Server notify to the client using below techniques,

1. Polling

In this technique,

1. Client requests a webpage from a server using regular HTTP request.2. The requested webpage executes JavaScript which requests for updates from the server at regular intervals (say 1 seconds).3. The server checks for updates and responds back, just like normal HTTP response.

In Polling, client continuously polls server at
regular intervals, asking is there any updates for client?

Disadvantage: if there is no updates, then unnecessarily client sends request to server and it hits server performance.

2. Long Polling

In this technique,

1. A client requests a webpage from a server using regular HTTP request.2. The requested webpage executes JavaScript which requests for updates from the server.3. Now if server does not has any updates, then server does not immediately responds telling "no update" instead waits until there's new information available. When there's update available, the server responds with the updated information that help client on information say price has changed.4. The client receives updated price information and immediately sends another request to the server, re-starting the process.

Disadvantage: Server like Tomcat would spawned thread to handle for each request, that reserve resources, till its done. So the traffic is smaller, but eats resources fast (also block resources).

Websocket overcome issues present in Polling and Long Polling.One limitation with Websocket is that it is only supported with HTML 5 compliant browsers.

Lets see a sample application on updating total count of connected clients using WebSockets, AngularJS and Java.

Note: ServerPush.java use classes from websocket jar, So make sure you have websocker-api.jar present in tomcat lib folder, if not then download using below dependency,

javax.websocketjavax.websocket-api1.0

After setup, Run the application in Tomcat.

Open browser and try to access application "http://localhost:8080/AppName"

Now, open firefox browser and try to access application "http://localhost:8080/AppName"

In the browser you will observe that as and when number of connected clients increase, the server pushes the total connected client count information on browser as well without explicit request from client

2
comments

Post a Comment

I'm Jayesh Patel, author of "JavaByPatel". I'm not a professional blogger but when time permits, love to share in-depth solutions to common Interview questions asked.Any questions/feedback, Please drop a mail atjayeshmaheshpatel@gmail.com