The Broadcast Server enables real-time communication using WebSockets. Clients can connect to the server, creating a persistent connection that allows data to be transmitted seamlessly between both sides.
- WebSocket Broadcasting: WebSocket protocol is used for bidirectional communication between the server and clients.
- Connection Management: Clients connect to the server by creating a new WebSocket instance (
new WebSocket(ws://)
). - Ping-Pong Mechanism: Long polling ensures the connection stays active, with periodic ping-pong messages to check if everything is still live.
- User Interface: A simple interface for user input is created using Node.js
readline
.
WebSocket is a protocol that supports two-way communication over a single, long-lived connection. After a connection is made, the server sends an HTTP response with status 101 Switching Protocols, upgrading the connection to WebSocket.
- Client-Server Interaction: Once the WebSocket connection is established, both the server and client can send and receive messages in real-time.
- Ping-Pong: The ping-pong mechanism keeps the connection alive. The server and client send ping and pong messages to make sure the connection is still active and can handle broadcasting messages.