How to install Socket.IO in Node.js

In this tutorial I'm going to explain you how to install Socket.IO in Node.js. In my next tutorials I will explain how to use Socket.IO with Node.js.

Now I will follow the Socket.IO's documentation from here:

  • Install Node.js.
  • Create a new folder and create a file app.js.
  • Run Command Line and enter npm install
  • Open your app.js file and add this code:

var app = require('http').createServer(handler)
var io = require('')(app);
var fs = require('fs');


function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
return res.end('Error loading index.html');


io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {

  • Create a file index.html in the same folder.
  • Add this code in your index.html file:

<script src="/"></script>
var socket = io('http://localhost:3000');
socket.on('news', function (data) {
socket.emit('my other event', { my: 'data' });

  • Open the command line and enter: node app.js
  • Then, in your browser go to http://localhost:3000/

Video tutorial:

Leave a Comment