WebSockets & Bidirectional Communication
Learning Objectives
- You can explain when a two-way WebSocket connection is useful.
- You can choose between polling, SSE, ordinary HTTP and WebSockets for an interaction.
SSE carries observations from server to browser while ordinary HTTP requests carry actions back. A WebSocket starts with an HTTP upgrade and then provides a persistent channel in both directions. Frequent interactive updates, such as shared drawing cursors, can use that channel.
Choose from the application’s communication pattern:
| Interaction | Communication pattern | Suitable starting point |
|---|---|---|
| A status display watches occasional changes. | Server to browser, with recovery reads. | Polling when its delay is acceptable, or SSE for prompt observations. |
| A bidder submits an occasional action. | A protected action needs an accepted/rejected result. | Ordinary HTTP, alongside live observations if useful. |
| Shared drawing cursors move frequently. | Both peers send frequent updates through the server. | A two-way WebSocket channel. |
An open connection establishes a delivery path. Business acceptance still needs authorization, validation and persistence. Resource limits and cleanup also remain necessary whichever channel is chosen. The live-listing exercises use SSE and HTTP.
Choose a two-way channel
0 / 5 points
In a remote rehearsal, each browser sends frequent temporary instrument-meter updates to a server. The server pushes the other musicians’ meter updates back through the same long-lived connections. Recordings are saved separately through protected HTTP requests. Which transport directly fits the live meter exchange?
The optional background contains a complete echo example with local observation instructions.
Check Your Understanding
- What does a WebSocket add to SSE plus ordinary HTTP requests?
- Which property of shared drawing cursors makes a two-way channel useful?
- Why does an open WebSocket connection not establish that an operation was accepted or stored?