Socket.IO and WebSockts

Currently I am experimenting with a data-intensive application built using web technologies. The biggest challenge is to reflect in UI real-time changes to the data stored on the server.

First implementation

I implemented long polling where the client polls the server, requesting new information. The server receives the request from the client, keeps it open and responds once new data is available. This implementation is suitable for small projects but can become a problem for high scalable applications with numerous clients and frequent changes to data.

Second implementation

The second approach is to implement bidirectional communication between server and client and allow the server to push notification to the client only when data changes. In this context one can use WebSocket-s.
WebSocket (https://en.wikipedia.org/wiki/WebSocket) is a computer communications protocol, providing full-duplex communication channels over a single TCP.

A convenient abstraction around the WebSocket protocol is Socket.IO library/module (https://socket.io/).

Socket.IO

Socket.IO enables real-time, bidirectional and event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.

In the context of Node.js, Socket.IO provides a consistent interface both in the browser and on the server for performing bidirectional communications.

Socket.IO provides an abstraction over the various methods used to maintain an active connection between a browser and a server. It will use WebSocket where the protocol is supported, and will transparently fall back to several other techniques in cases where WebSocket is not yet supported or the firewall introduces some limitations.

Below you can find a working example for demonstrating the bidirectional communication between Client and Server using Socket.IO.

The code is available also on Git: https://github.com/luckpp/socket-io-demo.

Demo

Open the Terminal and create the project structure:

$mkdir socket-io-demo
$cd socket-io-demo
$npm init

Install the Socket.IO module:

$npm install socket.io

Add the server side code:

$nano index.js

Add the client code:

$nano index.html

Start the server:

$node index.js

Open the Browser and navigate to this URL: http://localhost:4200/

socket.io.browser

Advertisements

Node Version Manager (nvm)

Node.js

I have recently installed Ubuntu 18.04.1 LTS. Still I haven’t given up on Windows as the default OS and I am using VirtualBox (https://www.virtualbox.org/) hypervisor in order to create the underlying virtual machine.

My goal in the next few months is to experiment with several cool technologies:

  • Node.js
  • MongoDB
  • Docker
  • and Linux

As you all know Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It actually consists of three different parts:

  • libuv: a high performance, cross-platform evented I/O library
  • V8: Google’s JavaScript engine (also used in Chrome)
  • js & C++ code: developed specifically for the Node platform

Installing Node.js on Linux

There are several ways to install Node.js on Linux :

1. Directly download it from the official website (https://nodejs.org/)

2. Use Node Version Manager or ‘nvm’ (https://github.com/creationix/nvm)

nvm is a tool that allows one to manage multiple Node.js versions installations in a Linux or Mac environment.

In order to install nvm, open the terminal an run the following commands:

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

$ command -v nvm

Close your current terminal, open a new terminal, and nvm command should be available.

After nvm has been installed and ready to use, try to list all installed Node.js versions:

$ nvm ls

node -> stable (-> N/A) (default)
iojs -> N/A (default)

In order to install the latest version of Node.js (for me is 10.14.2):

$ nvm install v10.14.2

Once again list the installed versions of Node.js:

$ nvm ls
-> v10.14.2
default -> v10.14.2
node -> stable (-> v10.14.2) (default)
stable -> 10.14 (-> v10.14.2) (default)

You can double check the current version of Node.js:

$ node -v
v10.14.2

And of course npm (Node Package Manager) has been installed:

$ npm -v
6.4.1

To open the Node.js REPL (Read-Eval-Print-Loop):

$ node
> console.log("Hello REPL!");
Hello REPL!
undefined

In order to leave the Node.js REPL hit ^C (CTRL+C).

There are several other useful nvm commands. For example one can chose to install an older version of Node.js (v10.14.0) and use it as default:

$ nvm install v10.14.0
$ nvm alias default v10.14.0
$ nvm ls
-> v10.14.0
v10.14.2
default -> v10.14.0
node -> stable (-> v10.14.2) (default)
stable -> 10.14 (-> v10.14.2) (default)

And now the default version is v10.14.0.
Note that versions v10.14.0 exist side by side and can be used interchangeably.