Summary
In this chapter, you learned how to implement a simple monitor server that pushes data changes in real time to the client. We used Node.js because of its nonblocking I/O behavior and because of the fact that we can write the server code in JavaScript.
After installing Node.js and creating a package.json
file for the application, we installed the express
and serve-static
web framework via the npm
Node package manager.
We used the serve-static
package to serve all the static files in the root directory of our application and added it as a middleware to express
. This allows you to write a simple file server in less than 10 lines of code.
To implement real-time communication, we need a bidirectional connection rather than the unidirectional HTTP protocol. Therefore, we added Socket.IO, a wrapper and compatibility layer for the WebSockets protocol, a protocol for bidirectional TCP connection for the Web. With Socket.IO, we can easily receive data from a client or—the big advantage to HTTP...