We will now implement our second Python-based server. Our overall outcome in this section will be similar to our RESTful API server and client that we created in the previous section—that is, we will be able to control our LED from a web browser. Our objective this time around, however, will be to create our program using a different technological approach using Web Sockets as our transport layer.
Web Sockets are a full-duplex communication protocol and are a common technology choice where real-time client/server interaction is required. Web Sockets are a technology that—in my opinion and experience—is best learned through doing rather than reading, especially if you are new to server development. A deep discussion of Web Sockets is beyond the scope of this chapter; however, you'll find two links in the Further reading section covering the basics.
If you are new to Web Sockets, I highly recommend reading those two resources as a primer before proceeding. And don't worry if the content does not sink in initially because I'm confident that, once you have used and understood how our Python Web Socket server and the accompanying Web Socket-enabled web page is implemented, the pieces of the larger Web Socket puzzle will start to come together.
For our Web Socket sever build, we will use the Flask-SocketIO library, which is modeled after and compatible with the popular Socket.IO library for JavaScript (https://socket.io).
We will start by running and using our Web Socket server to interact with the LED before proceeding to review the server's source code.