DEV Community

Luccasss
Luccasss

Posted on • Edited on

Some stuff and WebSocket

As a good developer I've been improving myself and I've noticed how important is the deepening of specifics topics.

Yeah, sometimes I just need a few minutes conversation to understand something that I wasn't and thats the point!
We have to know how to learn and later, we have to improve ourselves and our methods.

I'm far from being the best developer, but I always try to do my best and help people as much as I can.

Now, let's go dive into what really matters: WebSockets

What is WebSocket?

WebSocket is a protocol that makes possible bidirectional comunication between browser and server, like a tunnel that transports information in a continuously way.

image

What is the difference from http method?

HTTP flow: There is a browser and a connection is created when you use it and a request is sent to the website server. When the server gets the request, it will do the intern proccess and will give back the response in HTML format (usually).

The response is received by your browser and then it will not be able to request information from the server and vice versa.
If information is necessary, a new request will be done with the same pattern.

In this case there is a delay in each request and the HTTP method can be a problem to web applications that need request to server continuously. (e.g. Chatbot)

"But Luccas, I can create asynchronous call!"

Yes, but under the table HTTP protocol is there!

WebSocket comes to solve this communication problem. With WebSocket the connection between server and browser is continuously and faster than http and it is not necessary to create a new connection everytime that a new request is sent.

Curiosity

Its architecture is event based. The browser and server are going to receive events that are going to be transmitted to each other.

WebSocket x HTTP

HTTP example: While you're writing a message in a chat, the browser is constantly doing requests to verify if there is some messages to be delivered. When the message is delivered, the other user will only receive when the browser sends the next request to the server.
The problem is: The application always need to send a request even when there is not a message to be received. The flow is not constantly and it always depends on a requests that need to be done all time.
In a short: Requests are sent everytime to verify if a new message exists.

Websocket example: A persistent connection is made between browser and server to each user using it. When the message is sent to the server, it delivers the message even if the browser doesn't make the request. So, you can avoid the nonsense requests and consequently will not overload the application server.

Do I really need to use WebSocket?

WebSocket is compatible with several languages, like Java, C#, etc. But the main implementation is in NodeJS because of its architecture.

Like everything in technology we need to understand what is the best solution to solve our problems. WebSocket is not recommended for everything. It's really necessary a deep study about your problem and use WebSocket smartly, right?

If you need a bidirectional communication in a real time, go to WebSocket, otherwise go to HTTP and be happy =)

Top comments (2)

Collapse
 
gustavomagal profile image
Gustavo Magalhães da Silva Ribeiro

Pleasant! It is very good! Come on. 🚀🔥

Collapse
 
zaza profile image
Isaías Gonçalves Ribeiro

good explanation, well summarized and easy to understand