Node JS: Socket IO Module Example

Before reading this post, please read my previous posts.

denojs tutorialio_output3.png”/>

Introduction to Socket IO

Real-time client-server communication applications can be developed using the Socket IO Module. Real-time two-way communication between client and server is provided by internal model.

We can see that some of the technologies offer WebSocket Programming.

TheSocket IO Module is a part of the Java EE 7 WebSocketAPI.

Difference between Traditional HTTP and WebSocket Approaches

Traditional HTTP Approach:

The old and traditional approach to providing communication between client and server is still in use. When a client sends a request, the server pushes the response to the clients.

Communication between client and server can be provided using WebSocket Approach. WebSocket Approach provides communication between client and server by using a model called pulling that means the server always pulls data to clients. The more efficient and faster approach to provide client-server communication.

Advantages of Socket IO(WebSocket) Programming:

Faster and two-way communication.

Socket IO Setup

Unlike some of the default modules, such as “npm”, “http”, and “events”, Socket.IO comes with basic installation of the platform. We need to do it ourselves.

We have already talked about how to install a jsm module. If you’re not familiar with this command, please read my previous post.

To install Socket.IO globally

The first thing we need to do is use the “npm install” command.

npm install -g

Here is the meaning of the word “” and the meaning of the word “g”

To verify installation

We need to check whether the module is installed successfully or not. We can find a new folder if it’s installed correctly.

ode_modules\ as shown below:

Socket IO Example

  • Please refer to my previous post for more information on how to create a Node.js:Express JS Project.

There is a dependency on the package.json file.

“name”, “version”, “private”, true, “scripts”, “start”, “dependencies”

“name”, “version”, “private”, true, “scripts”, “start”, “dependencies”

  • Observe default “node_modules” folder

If we look at our project now, it has all the default Express JS Module libraries.

  • Update “node_modules” with “” library.

The following command is needed in order to use the library.

npm install –save

You can save the library from our Local repository. It’s similar to adding JAR files to our project.

You can open the command prompt and execute it at the root directory.

NOTE:- Please ignore those errors or warnings.

If you want to see the newly added library to our project, refresh your project in the studio and observe it.

  • Update app.js with the following content:

var path is required, var http is required, var favicon is required, var logger is required.

Line No 1-9, Line No 12-13, Line No 16 and Line No 17 are used to import dependency modules. We can set our port number here. Line No 20-21 defines our application view template engine We need to export our application.

  • Update index.jade file with the following content:

H1 is title p Welcome to span.received-message.

  • Update index.js file with the following content:

var express is a requirement, var router is a function.

  • Final project structure looks like this:

Execute Socket IO Example

  • You can open the command prompt and execute it below.

Run Socket IO Example and observe the output

Access application using this URL: https://localhost:3000/

You can see WebSocket power internally by using Mouse Right Click and selectInspect Element.

You can type some text in the box and see the output.

Each and every character is sent to the server. That is the beauty of web sockets.

That’s it about Express: Socket IO Module Example.

In my next post, I will give you another example of an IO module.

Leave a Reply

Your email address will not be published.

You May Also Like