Bachalo
Bachalo

Reputation: 7219

Simple NodeJS socketIO server throws error

Trying to debug this simple NodeJS socketIO server.

I keep getting the following error message on launch. I can't see anything wrong with the code.

Can anyone help?

TypeError: listener must be a function at TypeError () at Namespace.EventEmitter.addListener (events.js:130:11) at Server.(anonymous function) [as on] (/Users/foo/bin/node_modules/socket.io/lib/index.js:364:16) at Object. (/Users/foo/bin/foo.js:11:4) 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)*

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

io.on("connection", handleClient);


var handleClient = function (socket) {
    // we've got a client connection

    socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};

app.listen(8080);

Upvotes: 1

Views: 365

Answers (2)

Osama Bari
Osama Bari

Reputation: 598

you have forgotten to take an instance of express. I hope my below example will help you.

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

io.on("connection", handleClient);


var handleClient = function (socket) {
    // we've got a client connection

    socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};

app.listen(8080);

Upvotes: 2

Matt Harrison
Matt Harrison

Reputation: 13587

You're missing some parens:

var app = require('express')();

Upvotes: 2

Related Questions