JavaScriptMedium
Explain the concept of the Web Socket API
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
The WebSocket API provides a way to open a persistent connection between a client and a server, allowing for real-time, two-way communication. Unlike HTTP, which is request-response based, WebSocket enables full-duplex communication, meaning both the client and server can send and receive messages independently. This is particularly useful for applications like chat apps, live updates, and online gaming.
The following example uses Postman's WebSocket echo service to demonstrate how web sockets work.
js
// Postman's echo server that will echo back messages you send
const socket = new WebSocket("wss://ws.postman-echo.com/raw");
// Event listener for when the connection is open
socket.addEventListener("open", function (event) {
socket.send("Hello Server!"); // Sends the message to the Postman WebSocket server
});
// Event listener for when a message is received from the server
socket.addEventListener("message", function (event) {
console.log("Message from server ", event.data);
});
