Monday, June 29, 2015

"Socket.IO Real-time Web Application Development" - book review

In this post I will share my thoughts on a book called "Socket.IO Real-time Web Application Development" written by Rohit Rai, published in 2013.
It has 6 chapters and 2 appendices in around 120 pages. This book is made for developers who want to start developing highly interactive and real-time web applications or multiplayer web based video games. The book starts with brief introduction to real-time server side of web applications. How it was and how it is now.
It then continues with building a simple chat application using NodeJS, ExpressJS, JQuery, Socket.IO and a templating engine called Jade. It is expected that the reader understands well JavaScript. Also NodeJS knowledge would be a plus but it is not necesarry as all needed aspects are well explained. 

Chapters:
  1. Going Real-Time on the Web. Covers history of XMLHTTPRequest, AJAX, FlashSocket, etc... and of course introduction to real-time web of today.
  2. Getting Started with Node.js. Covers brief introduction to Node.js programming. How to install it, how to import npm packages, and how to create a simple "hello world" application.
  3. Let's Chat. Explains how to start with Socket.IO on the server side but also on the client side. Some basic information what is what on the client/server and how it works.
  4.  Making it More Fun. This chapter is the longest one and I would recommend passing it in 2 or 3 parts with breaks between. It adds nicknames to chat users and "create rooms" functionality.
  5. The Socket.IO Protocol. Advantages od Socket.IO over WebSockets API. Socket.IO is much more capable and complex, therefore it needs dedicated protocol to function. It also covers all protocol types of messages that exist.
  6. Deploying and Scaling. Covers some information on how to keep your app on Node.js up and running and how to monitor background processes with Monit and Forever. Scaling with HAProxyNode Cluster and Redis.
  7. Appendix A: Socket.IO quick reference.
  8. Socket.IO Backends. Covers brief information about Socket.Io backends for Erlang, Google Go, Java, Perl and Python.
 The problems I faced while developing the sample chat application were:
  1. The book is made for Socket.IO version 0.9 which is discontinued and no longer available for download over npm. So I downloaded version 1.3.5.
  2. The protocol API is a bit different from version 0.9 to 1.3.5 so if you get deep into Socket.IO structure you'll be a bit confused.
  3. The web documentation is kind of poor so you will need to search the web for information how to get valuable data from the Socket.IO Server object.
My thoughts on the book, it is well made for what it was meant for, but the problem with these JS libraries is that they change very fast, so this book is getting old already. On the other hand it gives a good example to practice with newer versions of Socket.IO so therefore I recommend it for new Socket.IO developers.
There were some inconsistencies but all in all it serves its function. I give 4.2/5.

I uploaded the demo chat application on this location:
Its not finished, because I have other things to do at the moment, but it is good start. I used https://www.openshift.com/ with Node.js 0.10 with preinstalled HAProxy load balancer. 
If it is unavailable the service sets the app to idle state. So remind me or write an email if it stopped, to restart it.

Notes:
  •  Socket.IO creates a room for each tab you have on every browser. So if you have two tabs on Chrome and three tabs in Firefox connected to Socket.IO server, there will be 5 rooms generated for you inside the server state. 
  •  Socket.IO creates unique id for each socket connected to the server and that same id will be for a pregenerated room for the socket. You can create your room with custom name, and the socket you are using will be in two rooms, yours and the pregenerated one.
  •  Be careful how you connect to namespaces because the internal server state might get mess and you cannot get information properly. 
  • Socket.IO is easy to code. The API is same on both server and client.It works siemlesly though it is over HTTP layer.
Useful links I found on the web about Socket.IO:

https://github.com/SocketCluster/socketcluster
https://en.wikipedia.org/wiki/WebSocket
https://forums.openshift.com/keep-getting-program-node-serverjs-exited-with-code-8-and-503-service-unavailable
http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/
https://www.npmjs.com/package/roomdata
http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/
http://howtonode.org/websockets-socketio
http://coenraets.org/blog/2012/10/real-time-web-analytics-with-node-js-and-socket-io/
http://ejosh.co/de/2015/01/node-js-socket-io-and-redis-intermediate-tutorial-server-side/
https://en.wikipedia.org/wiki/Appserver.io