Introduction to WebSockets

Get an introduction to WebSocket technology using a sample application based on Socket.io.

by Gigi Sayfan

Jan 25, 2017

In this article, I'll introduce you the WebSocket technology using a sample application based on Socket.io. At the end of this article you will have a good idea what WebSockets are all about, why it's needed, how it works, go through a simple but illustrative demo application client-server application.

Why WebSockets?

The Web was built around HTTP, which is a request-response protocol. Traditionally, every client request opened a new TCP connection to the server, waited for response and closed the connection. Establishing a TCP connection is relatively slow and expansive process. That limited the amount a requests a server can handle as well as the latency of individual requests. In addition, HTTP carries a lot of overhead with its cookies, headers, etc. One last problem is that the server can't push data to the client. Various solution and workaround were developed. Frequent polling by the client allows simulating server push, but you trade of the resolution of the updates with the overhead of many poll requests. Other solutions like long polling kept the same TCP connection open, but are cumbersome and suffer from various downsides. WebSockets were designed to provide a consistent low-latency persistent connection between browser (or any WebSocket client) and a server.

What Is a Web Socket?

WebSocket is a protocol that establishes a two-way TCP connection between client and server. While TCP is stream-based (you just get a stream of bytes), WebSocket is message-based. You get complete message, which is much nicer to work with because someone else (the WebSocket implementation) does all the framing and buffering for you.

How do WebSockets Work?

The HTTP server does an HTTP handshake with the client and upgrades the connection to a WebSocket connection over port 80 (good for penetrating firewalls).

Tic-Tac-Toe Sample App

The sample application is a two-player tic-tac-toe game based on Socket.io and Node's Express Web framework. Here is the server's package.json file

Next, initialize some state for the game. The server manages only one game at a time. The players dictionary maps the 'x' and 'o' players to their corresponding WebSockets (initially null). That way when a message arrives from a particular WebSocket, the server knows which player is it. The player variable initialized to 'x' keeps track of the next player to play.

This is the core processing loop. Inside the io.on('connection') call a few socket event handlers are set. The 'click' event handler is the most interesting. When it is received the server checks that it's the right player, that the square is not already occupied and that the game is not over. If everything is OK it updates the board, sends it to the players and checks if the player won. If the player won it sends a 'victory' message to all players and the state becomes "game over". If the game is still going it toggles the player, so the other player an play.

Now, we get to the client's logic. The call to "var socket = io();" connects to the server. There is no need to specify a URL or IP address. It just connects to its own Web server. Then it binds a click handler to every square in the board (<td> cell). The handler just logs its 'click' and sends a 'click' message to the server, with the ID of the clicked cell.

This is just demo app, so I didn't even handle the case in which there is no winner (very common in tic-tac-toe).

Conclusion

WebSockets allow sophisticated and efficient workflow that require bi-directional communication between Web servers and browsers. It is efficient and easy to work with. Consider it for your next Web application.

Gigi Sayfan is the chief platform architect of VRVIU, a start-up developing cutting-edge hardware + software technology in the virtual reality space. Gigi has been developing software professionally for 21 years in domains as diverse as instant messaging, morphing, chip fabrication process control, embedded multi-media application for game consoles, brain-inspired machine learning, custom browser development, web services for 3D distributed game platform, IoT/sensors and most recently virtual reality. He has written production code every day in many programming languages such as C, C++, C#, Python, Java, Delphi, Javascript and even Cobol and PowerBuilder for operating systems such as Windows (3.11 through 7), Linux, Mac OSX, Lynx (embedded) and Sony Playstation. His technical expertise includes databases, low-level networking, distributed systems, unorthodox user interfaces and general software development life cycle.