geekelo / dsa_practice

This repo is maintained just for the purpose of daily data structures and algorithm practice and proficiency
1 stars 0 forks source link

Web socket #15

Open geekelo opened 4 months ago

geekelo commented 4 months ago

WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection. It is designed to be implemented in web browsers and web servers but can also be used in any client-server or server-to-server scenario. Unlike traditional HTTP communication, which follows a request-response model, WebSocket allows for real-time, bidirectional communication between clients and servers.

Here are some key features and aspects of WebSocket:

  1. Full-Duplex Communication: WebSocket enables full-duplex communication, meaning both the client and server can send messages to each other independently at any time. This is in contrast to the traditional request-response model of HTTP.

  2. Persistent Connection: Once a WebSocket connection is established, it remains open, allowing for efficient communication without the overhead of opening and closing connections for each request.

  3. Low Latency: WebSocket reduces latency compared to traditional HTTP connections because it eliminates the need to establish a new connection for each communication. This makes it suitable for real-time applications where low latency is crucial, such as chat applications, online gaming, financial platforms, and live streaming.

  4. Lightweight Protocol: WebSocket has a lightweight protocol overhead, making it more efficient in terms of data transfer compared to HTTP, especially for scenarios with frequent small messages.

  5. Port 80/443 Compatibility: WebSocket connections can use the same ports as HTTP (ports 80 and 443), which are typically open in most network configurations. This makes it easier to deploy WebSocket applications without dealing with additional firewall settings.

  6. WebSocket API: Web browsers provide a JavaScript API called the WebSocket API that allows developers to establish WebSocket connections and handle WebSocket events in their web applications.

Here's a simple example of how WebSocket works in a web application using JavaScript:

// Client-side code
const socket = new WebSocket('ws://example.com/socket');

socket.addEventListener('open', (event) => {
  socket.send('Hello, server!');
});

socket.addEventListener('message', (event) => {
  console.log('Message from server:', event.data);
});

// Server-side code (in a WebSocket server implementation)
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8080 });

server.on('connection', (socket) => {
  console.log('Client connected');

  socket.on('message', (message) => {
    console.log('Message from client:', message);
    // Process the message and send a response if needed
    socket.send('Hello, client!');
  });
});

WebSocket is widely used for building real-time web applications where instant communication and data updates are crucial. It provides a more efficient and responsive alternative to traditional HTTP-based communication for scenarios that require constant data exchange between clients and servers.