Hardware & IoTLesson 103Free preview
RasPi WebSocket
This lesson is part of Node.js · All levels
Summary
This lesson builds a complete application: a web server on Raspberry Pi that controls GPIO via WebSocket so that the button/LED status appears in real time in the browser. You're going to combine the HTTP, fs, and ws modules to present a page as a two-way message channel. This pattern became the basis of the home IoT dashboard.
Key points
- One server serves a static page and a WebSocket connection on the same port. (index.html)
- The event watch button is sent to the browser via JSON.
- The browser's commands (e.g. {'action':'led','value':1}) are received via ws.on('message').
- Broadcast to many browser tabs using iterations over connected clients.
- Use a ping/pong heartbeat to detect and clear the dead connection.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.