0

I am learning to use socket.io from its web website Socket.IO. Everything went fine, but after I add the 3rd line var io=require('socket.io')(http); I got the error when trying to run the server:

C:\Users\user\chat-example\index.js:3
var io = require('socket.io')(http);
                             ^
TypeError: object is not a function
    at Object.<anonymous> (C:\Users\user\chat-example\index.js:3:30)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

This is the code as in the Socket.IO

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

What's wrong? This is my first time learning nodejs and socket.io. So I would really appreciate if you could kindly help me understand and solve the problem. Thank you.

Kakar
  • 5,354
  • 10
  • 55
  • 93
  • 2
    That error will occur if you've installed an out-dated version of `socket.io`. You'll want to upgrade to at least `1.0.0` to be compatible with the documentation. The most recent release is [`1.2.1`](https://www.npmjs.com/package/socket.io). – Jonathan Lonowski Dec 28 '14 at 11:56
  • @JonathanLonowski Ok. I did install it using `npm install socket.io@"~0.8.1"`. But when I check it using the npm `npm info socket.io version` it shows 1.2.1. – Kakar Dec 28 '14 at 11:59
  • `npm info` shows information from the remote repository. The `version` will be the most recent release. [You can see what version you have installed with `npm ls`.](http://stackoverflow.com/questions/10972176/find-the-version-of-an-installed-npm-package) – Jonathan Lonowski Dec 28 '14 at 12:05

1 Answers1

0

That error will occur if you've installed an out-dated version of socket.io. You'll want to upgrade to at least 1.0.0 to be compatible with the documentation. The most recent release is 1.2.1. – Jonathan Lonowski

Armali
  • 18,255
  • 14
  • 57
  • 171