Richard Abear
Richard Abear

Reputation: 186

socket io .use() is not a function

Im trying to use sessions middleware to connect express and socket.io

However i get this error:

io.use(function(socket, next){
^

TypeError: io.use is not a function
at Object.<anonymous> (/home/ubuntu/workspace/newserver.js:30:4)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:990:3

Here is my code:

var http = require('http');
var path = require('path');

var async = require('async');
var express = require('express');
var socketio = require('socket.io');
var sessions = require('express-session');
var mysql = require('mysql');
var RedisStore = require('connect-redis')(sessions);
var bodyParser = require('body-parser');

// App declaration
var router = express();
var server = http.createServer(router);

var io = socketio.listen(server);
io.set('log level',0);
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended:true}));



var sessionMiddleware = sessions({
    store: new RedisStore({}),
    secret: '901uj0394-0i4-#',
    resave:true,
    saveUninitialized:true
});


router.use(sessionMiddleware);




var session;

var appConfig = {
    title: 'The Warring States 2'
};

router.set('views', path.resolve(__dirname,'client/views'));
router.set('view engine', 'pug');

var connection = mysql.createConnection({
    // SQL Information
        host: 'localhost',
        user: 'boyton',
        password: '',
        database: 'WarringStates'
});

connection.connect(function(error){
    // callback
    if(!!error) {
        console.log('Error!');
        console.log(error);
    }
    else {
        console.log('Connected to the database');
    }
});


// Socket
var sockets = [];
var gamelobbies = [];

Im only starting out with node and expresss, I created a c9 container for node and installed the default Packages to work with the stack, Ive "tried" to update node and npm and express as well. However im not sure if they're up to the latest versions

here is what i get when i invoke check-node-versions

node: 4.7.3
npm: 2.15.11
yarn: not installed

express version 3.6.2

any help and input would be great. Thanks guys.

Upvotes: 0

Views: 1600

Answers (1)

jfriend00
jfriend00

Reputation: 707328

Change this line:

var io = socketio.listen(server);

to this:

// create an instance of socket.io, bound to our web werver
var io = socketio(server);

And, then make sure you have this somewhere:

// start the web server
server.listen(80);

The socketio library is a constructor and it wants you to call it and pass it the server to create the io object (an instance of the socket.io server). You then separately start the server so it can be used for both your http requests and your socket.io requests.

The way you were doing it, socketio was not the right kind of object to call .listen() on. It was the module handle rather than an instance and thus io was the wrong kind of object and thus why there was no .use() method.

Upvotes: 2

Related Questions