Middle+
Tell me about WebSocket: how it works, what data can be transmitted, encryption?
sobes.tech AI
Answer from AI
WebSocket is a protocol that provides a bidirectional persistent connection between a client and a server over TCP. After establishing the connection (handshake via HTTP), the client and server can exchange messages in real-time without the need for repeated HTTP requests.
Any data can be transmitted in the form of text (UTF-8) or binary messages (e.g., ArrayBuffer, Blob). This is convenient for chats, games, financial applications.
Encryption is provided when using WebSocket Secure (wss://), which operates over TLS, similar to HTTPS. This protects data from interception and tampering.
Example of creating a WebSocket on the client:
const socket = new WebSocket('wss://example.com/socket');
socket.onopen = () => {
socket.send('Hello server!');
};
socket.onmessage = event => {
console.log('Received:', event.data);
};