# WebSockets

HTTP is a question and an answer; a WebSocket is a phone line that stays open. Learn what WebSockets add, open a first connection between a ws server and the browser's WebSocket, send JSON messages both ways, and broadcast to every player at once.

Updated: November 19, 2026

## [What WebSockets Add](https://www.ask-codey.com/courses/nodejs-in-depth/topics/node-websockets-basics)

1. [What WebSockets Add to HTTP](https://www.ask-codey.com/lessons/node-websockets-what-they-add) (Journeyman): HTTP answers only when the client asks, so a page that must hear about a goal the moment it's scored either asks again and again, which is polling, or opens a WebSocket, one connection that stays open so the server can speak first.
2. [A First WebSocket Connection](https://www.ask-codey.com/lessons/node-websockets-first-connection) (Journeyman): The ws package puts a WebSocketServer on the same server and port as an Express app, its connection, message and close events let the server hear from each client and answer it, and the browser's own WebSocket connects a page, with JSON messages whose type says what each one is.

## [Many Listeners](https://www.ask-codey.com/courses/nodejs-in-depth/topics/node-websockets-many-listeners)

1. [Broadcasting and Rooms](https://www.ask-codey.com/lessons/node-websockets-broadcasting) (Journeyman): A ws server broadcasts by sending one message to every open client in wss.clients, greets a client who connects late with the current state, keeps rooms as a Map of Sets so a message reaches only one group and cleans up when a client closes, and is tested with real ws clients on a port the test picks.

## [Exercise: Live Match Scoreboard](https://www.ask-codey.com/courses/nodejs-in-depth/topics/exercise-node-live-match-scoreboard)

1. [Exercise: Live Match Scoreboard](https://www.ask-codey.com/lessons/exercise-node-live-match-scoreboard) (Journeyman): Write the live line behind Hilltop Intramurals' scoreboard with the ws package, so every goal reaches every open page at once, a fan who connects late sees the score straight away, bad messages get an error back, and everyone sees how many are watching, with real WebSocket clients in the tests.

Source: https://www.ask-codey.com/courses/nodejs-in-depth/chapters/node-websockets
