
30/10/2024
WebSocket, introduced as part of HTML5, is designed to facilitate a persistent, low-latency, two-way communication channel between a web browser (client) and a server. Unlike traditional HTTP requests, which are request-response based and generally involve higher overhead, WebSocket provides a more efficient way to exchange data in real time. Let’s delve into how it works, step by step.
Initiation: The communication begins when the client sends a WebSocket handshake request to the server. This handshake is an HTTP request that includes a special header to inform the server that the client wishes to establish a WebSocket connection.
const socket = new WebSocket('ws://example.com/socket');
Server Response: Upon receiving the handshake request, the server acknowledges it and responds with a handshake response. If the server supports WebSockets, it upgrades the connection from HTTP/1.1 to a WebSocket connection.
Connection Established: After the handshake is successfully completed, the WebSocket connection is opened. At this point, both the client and server can send messages to each other freely over this single connection.
WebSocket operates over a specific protocol that is different from HTTP. Here are the key features:
Full-Duplex Communication: WebSocket allows simultaneous two-way exchanges of messages between client and server. This is in contrast to HTTP, which is half-duplex (one direction at a time).
Lightweight Messages: Once the connection is established, messages are sent in the form of frames (binary or text), utilizing a small header format, which reduces the overall payload size compared to traditional HTTP requests.
Persistent Connection: The WebSocket connection remains open, allowing for communication at any time until either the client or server explicitly closes it. This eliminates the need for repeated handshakes.
Once the connection is active, both parties can send and receive messages at will.
Sending a Message: The client can send data like this:
socket.send('Hello Server!');
Receiving a Message: The server sends messages back to the client, which can be handled using an event listener:
socket.onmessage = function(event) { console.log('Message from server: ', event.data); };
WebSocket connections can encounter issues or be closed intentionally, which is why handling errors and connection closures is vital:
Error Event: To handle errors, you can listen for the error event:
socket.onerror = function(error) { console.error('WebSocket Error: ', error); };
Close Event: For when the connection closes (either normally or due to an error), you can monitor the close event:
socket.onclose = function(event) { console.log('WebSocket closed: ', event.code, event.reason); };
Using WebSocket offers multiple benefits:
Overall, WebSocket revolutionizes how web applications communicate with servers by enabling efficient, real-time interactions suitable for today’s dynamic web ecosystem. By understanding how WebSocket functions within HTML5, developers can create more interactive and responsive applications that significantly enhance user experience.
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5