Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js w/ socket.io : TypeError: Obj has no method 'on' at new Manager

Following the example here and running into trouble.

I installed socketIO with npm install socket.io When I run node server.js the following exception occurs:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object #<Object> has no method 'on'
    at new Manager (/Users/x/nodeprojects/cloudnode/node_modules/socket.io/lib/manager.js:97:10)
    at Object.listen (/Users/x/nodeprojects/cloudnode/node_modules/socket.io/lib/socket.io.js:71:10)
    at Object.<anonymous> (/Users/x/nodeprojects/cloudnode/sky/server.js:12:26)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)
    at EventEmitter._tickCallback (node.js:126:26)

server.js:

var static = require('./plugins/node-static');
var socketIO = require('socket.io');
var clientFiles = new static.Server('./client');

var httpServer = require('http');
httpServer.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) {
    client.send('Please enter a user name ...');

    var userName;
    client.on('message', function(message) {
        if(!userName) {
            userName = message;
            webSocket.broadcast(message + ' has entered the zone.');
            return;
        }

        var broadcastMessage = userName + ': ' + message;
        webSocket.broadcast(broadcastMessage);
    });

    client.on('disconnect', function() {
        var broadcastMessage = userName + ' has left the zone.';
        webSocket.broadcast(broadcastMessage);
    });
});

I'm running node v 0.4.10 Anyone have an idea for a solution?

like image 314
Soroush Hakami Avatar asked Feb 23 '23 00:02

Soroush Hakami


1 Answers

You're passing the http module to socket.io instead of the http server.

var http = require('http');
var httpServer = http.createServer(function (request, response) {
  request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) {
like image 88
fent Avatar answered Feb 24 '23 13:02

fent