🌐 Web Development

WebSocket

WebSocket

A bidirectional real-time communication protocol. Unlike HTTP, it keeps the connection open. Used for chat, games, and real-time notifications.

Detailed Explanation

WebSocket is a protocol for two-way real-time communication between a client and a server. Unlike HTTP, once a connection is established, both sides can freely exchange data.

It is essential for chat, real-time notifications, games, collaboration tools, and more.

Code Example

// 클라이언트 const ws = new WebSocket('wss://example.com/chat'); ws.onopen = () => ws.send('Hello!'); ws.onmessage = (event) => console.log('받음:', event.data); ws.onclose = () => console.log('연결 종료'); // Node.js 서버 (ws 라이브러리) const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { ws.on('message', (msg) => { // 모든 클라이언트에게 브로드캐스트 wss.clients.forEach(client => client.send(msg)); }); });

Practical Conversation Example

Planner

"When a new message arrives in the chat feature, it should show up right away."

Developer

"If we implement it with WebSocket, the server can push it right away. We also don't have to keep sending requests like with polling."

Related Terms

Learn More