For those who don't know what websockets is about, here is a brief introduction:
Wikipedia says "WebSocket is a protocol providing full-duplex communications channels over a single TCP connection", this means that in a client-server communication the client as the server can send a message leaving behind the request-response protocol, where the exchange is always initiated by the client (usually a browser) and the server cannot send any data without the client requesting it first.
It also says "WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application", websockets was introduced in the clients or browsers since the html5 specification.
If you want to know more about websockets refer to the following links:
http://www.websocket.org/
http://en.wikipedia.org/wiki/WebSocket
https://developer.mozilla.org/en/docs/WebSockets
WS
Let's start with the first module "WS" , to start using it it needs to be installed so the following commands install the library:
To install the module to the current directory use the following command:
$ npm install ws
To install the module in a global way use the following command:
$ npm install ws -g
Once install it we can create an example using this module, the code below shows how to create a simple server with ws:
Save the file as ws_server.js, from the code you can see that the server will be running in port 8080, any request to this port the server will attend it. To create a client use the code below:
Save the file as ws_client.js and now lets run them, to run the server just type the following commands:
Server:
$ node ws_server.js
Msg received in server: Msg from client
Client:
$ node ws_client.js
Msg received in client: Msg from server
From the output you can see that the server starts and listens to any message when the client starts it also listens to messages and it sends a message, then the server receives it and answers this message
To find more about ws module go the following link https://github.com/einaros/ws is the page of the library.
WebSocket
Lets try another module for websocket, the WebSocket-Node module, this module has more requirements, needs c++ and python to compile the native modules but they are optional but its efficiency decreases if they are not installed, if you are a windows user you will find harder to install.
To install WebSocket-Node module use the following command and use -g option to install it globally:
$ npm install websocket -g
Once installed the module lets proceed to create the example:
Save the file as websocket_server.js. It creates a server for websockets that listens requests at the url ws://localhost:8080/example, in the code you can see that the port is configured at the instruction server.listen(8080), and the path is formed with request.accept('example', request.origin); .
Let's create a client for the server:
Save the file as websocket_client.js, from the code you can see that the client creates a connection to the server using the instruction client.connect('ws://localhost:8080/', 'example'); ,when a connection is done a message is sent to the server and the server should respond to the client with also a message.
Now lets run the scripts using the following commands:
Server:
$ node websocket_server.js
Received Message: Message from client
Client:
$ node websocket_client.js
Received: 'Msg from server'
The messages that the server and client are sending should be printed in the console if everything works as expected, if a problem happens with the connection you should see a message like this "Connection Error: Error: connect ECONNREFUSED".
To find more about websocket module go the following link https://github.com/theturtle32/WebSocket-Node is the page of the library.
engine.io
Another module to use for creating websockets is engine.io, although this module is part of a bigger framework you can still use this module to create websockets.
To install the module use the following command, to set it globally use the -g option:
$ npm install engine.io -g
To create a client for this module the module "engine.io-client" is required, lets install the module:
$ npm install engine.io-client
Lets create an example of a server:
Save the file as engineio_server.js, from the code you can see the configuration needed to run the server, it is pretty simple.
Now lets create a client for this server:
Save the file as engineio_client.js and lets run the example with the following commands:
Server:
$ node engineio_server.js
Msg Received at server:Msg from client
Client:
$ node engineio_client.js
Msg Received at client:Msg from server
As in the others examples if everything works as expected you should see the messages exchanged by the server and the client in console.
To find more about engine.io module go the following link https://github.com/socketio/engine.io is the page of the library.
There are many other libraries that can be used to create websockets with nodejs, every one with its pros and cons but it would take another post to discuss this topic. I guess the hardest part would be to choose which library to use.
I wish this post helped you to better understand what websocket is and how to implement it in nodejs.