JEET ADHIKARI
JEET ADHIKARI

Reputation: 539

Socket io implementation, Getting TypeError('"listener" argument must be a function')

I am trying to implement Socket IO with my angular Js project. I am new into this, please help.

This is my server.js file

var express = require('express');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = 8080;

app.use(express.static(path.join(__dirname, "app")));

io.on('connection'), function(socket){
    console.log('new connection made');
}

server.listen(port, function(){
    console.log('Listening to port '+ port);
})

I have copied the socket.io.js file from socket.io-client and put it in my lib folder. So my index.html has

<script src="lib/js/socket.io.js"></script>
//all other includes required

<body>
     //code here
</body>

heres is the error I get to see when I execute nodemon server.js

[nodemon] starting `node server.js`
events.js:216
    throw new TypeError('"listener" argument must be a function');
    ^

TypeError: "listener" argument must be a function
    at _addListener (events.js:216:11)
    at Namespace.addListener (events.js:275:10)
    at Server.(anonymous function) [as on] 
 (D:\SocketIOExperiment\ProjExperiment\node_modules\socket.io\lib\index
 .js:456:29)
at Object.<anonymous> 
(D:\SocketIOProject\SmartAdminExperiment\server.js:11:4)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
  [nodemon] app crashed - waiting for file changes before starting...

Upvotes: 0

Views: 1285

Answers (1)

vinoth h
vinoth h

Reputation: 531

Small typo on your code.Try this :

var express = require('express');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = 8080;

app.use(express.static(path.join(__dirname, "app")));

io.on(('connection'), function(socket){
    console.log('new connection made');
})

server.listen(port, function(){
    console.log('Listening to port '+ port);
})

Remember, io.on(event,cb) is a function call that registers the cb function to the event.

Upvotes: 2

Related Questions