WebSocket is a protocol that provides full-duplex communication channels over a single TCP connection between:
with lower overheads, facilitating real-time data transfer from and to the server.
WebSocket is running over a single TCP connection where:
Websocket is located at layer 7 in the OSI model (and, as such, depend on TCP at layer 4)
RFC 6455 states that WebSocket is designed to:
Similar two-way browser-server communications have been achieved in non-standardized ways using stopgap technologies such as Comet.
The WebSocket:
Push real-time updates such as:
var ws = new WebSocket('ws://host.com/path');
ws.onopen = () => {
// connection opened
ws.send('something'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log(e.data);
};
ws.onerror = (e) => {
// an error occurred
console.log(e.message);
};
ws.onclose = (e) => {
// connection closed
console.log(e.code, e.reason);
};