A Freestyle for Learning WebSockets In JavaScript.
What is WebSockets?
Are you new to JavaScript and a beginner in the programming language? Well, that’s good because in this article I am going to talk to you about an API connection between a web browser and a server. The connection between the two is called WebSockets. Websockets are some kind of communication between a client and a server that is bidirectional which means that the data can flow from both ways, can flow from the client to the server, and from the server to the client to the server. They can allow real-time data flow and real-time applications. So WebSocket is a two-way computer conversation protocol that is going over a TCP. This is used to handle high-scale data transfers between the server and client so that both can send data at any time, the reason why it's a real-time data flow.
Uses of WebSockets.
WebSockets are useful for multiplayer browser games, collaborative code editing, live test for sports or news websites, online drawing canvas, and real-time to-do apps with multiple users.
The process that comes from the WebSocket handshake, will begin with a client sending an HTTP request to the server. Now if you do not know about the HTTP request process please research up more information on what happens when the client computer sends a request to the server and HTTP request methods. Here is an example of how the client will open up a connection for the WebSocket.
If you are currently working with a software engineering Bootcamp and end up working on a sprint called Shortly Express, this may help you understand a few things. The WebSocket connection above is being opened to a website called websocket.example.com and its initial request headers would look like this. You have an upgrade header that the server will agree to an upgrade if it supports the protocol. The upgrade header informs the server that the client wants to add a WebSocket connection.
The example above shows how to implement the WebSocket connection in Node.JS. That sever will upgrade once the connection is accepted and the handshake is done, but that HTTP connection will be replaced by the WebSocket connection.
WebSockets always open.
Let’s say there is a chat application that is hosted on the server and all these different people that are on their own browser will go to a website they want to request this chat application in the browser. These people are viewing this chat application in the browser now. So let us say my friend Peter was to type something in the chat and clicked send, then the message that he typed is going to be updated in all of these different client computers that are out there in the world that are viewing this chat application as well. That is why WebSoctes is some type of action.
I hope this article gave you some insight on WebSockets of JavaScript and the flow of data between the client and the server.