how to start working with socket.io

how to start working with socket.io

Introduction

Socket.io is real time javaScript library which enables real-time bidirectional event-based communication.
It works on every platform, browser or device, focusing equally on reliability and speed. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Both components have a nearly identical API. Like Node.js, it is event-driven. You can create instant message , real time notification, real time data analytics etc.

Who can learn?

If you are a web developer or a student with knowledge of Node.js & JavaScript and want to create some really great real time application with nodejs and JavaScript then you can start working with socket.


Prerequisites

To start with socket.io should have at least basic knowledge of below.

You need to install latest version of node.js. It is opensource application. You can download it from official site for Wondow/Linux/SunOS. Download

Get started with Socket.io

To start working with socket.io make sure Node.JS is installed in your machine.And you can also check that Node.JS is installed or not in your machine by executing below given commands.

If these command executed successfully means Node.JS is successfully installed on your machine and now you are ready to start working with Socket.io.
Open you command prompt and enter the following command to create you project folder and initialize node in you project.

After execution of npm init it will ask you some questions that you need to answer. It will ask you , your project name, version, Author name, description etc.

socket.io installed

After this, It will create package.json file in your project folder which contents all your project detail as well as all the dependencies you installed.
In this tutorial i am going to use express.Now, in order to install express run below command in you project directory.

Now express is installed in my project now we can start writing code. In project folder, create on file index.js and will write the code to setup server and express.

Here i have used http and express to setup project. http module is used to create sever where we can run our application and express to manage request / response. This is the basic setup of project. Now we will check our project that it is running or not by executing below command in project folder.

After successful execution of this command you will found one output in command prompt.
socket.io success output

Now you can check in browse by pointing http://localhost:3000.

It was the basic project setup for socket.io. If you wan to learn more about chatting and real time messaging in socket.io then you can find example on GitHub. Download example.
For more detail you can check Socket.IO official site

Please follow and like us:

You May Also Like

About the Author: Sujeet Kumar

Sujeet is Web and Android Devloper with a soft spot for functional programming. I like to play cricket, regular workout.

Leave a Reply

Your email address will not be published. Required fields are marked *