Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why is io undefined?

Why is io undefined?

Problem

I have an express app using socket.io. I'm unclear on what is needed for the client to begin with, so I'd appreciate if anyone can show me how to do that. Additionally, I'm not sure if I have my client side code right. Here's my client socket code:

var socket = io.connect('http://localhost:3000');
    var tView = new totalViewModel();
    socket.on('connect', function (data) {
    function loggedIn(e) {
        e.preventDefault();
        var logged = {
            username: document.getElementById('pEmail'),
            password: document.getElementById('pWord')
         };
         socket.emit('login', JSONout(logged));
    }
    socket.on('uploadList', function (data) {
         tView.setNum(JSONin(data).total);
         ko.applyBindings(tView);
    });

    function logPurchase(e) {
         e.preventDefault();
         var purchObj = {
            price: document.getElementById('pPrice').value,
            priceName: document.getElementById('pName').value,
            entryDate: new Date()
        }
        socket.emit('purchaseLog', JSONout(purchObj));
    }

    function logDeposit(e) {
        e.preventDefault();
        var depositObj = {
             price: document.getElementById('dPrice').value,
             entryDate: new Date()
        };
        socket.emit('depositLog', JSONout(purchObj));
    }

    function getLogout(e) {
        e.preventDefault();
        socket.emit('goLogout', {
             none: 'none'
        });
    }

    function getIndex(e) {
        e.preventDefault();
        socket.emit('goIndex', {
            none: 'none'
        });
    }

    function getMonthly(e) {
         e.preventDefault();
         socket.emit('goMonthly', {
             none: 'none'
         });
    }

    function getYearly(e) {
        e.preventDefault();
        socket.emit('goYearly', {
            none: 'none'
        });
    }
    socket.on('wentMonth', function (data) {
         tView.setNum(total in JSONin(data));
         ko.applyBindings(tView);
    });
    socket.on('wentYearly', function (data) {
         tView.setNum(total in JSONin(data));
         ko.applyBindings(tView);
    });

    function register(e) {
         e.preventDefault();
         var reg = {
             username: document.getElementById('pEmail').value,
             password: document.getElementById('pWord').value,
             dateJoined: new Date(),
             total: 0,
             purchase: {},
             deposit: {},
             monthlyTotal: 0,
             yearlyTotal: 0
         };
         socket.emit('registered', JSONout(reg));
     }
});

Here's my server side code:

var express = require('express');

var app = express();

var mongojs = require('mongojs');

var http = require('http');

var db = mongojs('127.0.0.1:27017/mySpendingDB', ['users']);

var server = http.createServer(app);

var io = require('socket.io').listen(server);

var path = require('path');

app.configure( function () {
   app.set('views', __dirname + '/views');
   app.set('view engine', 'ejs');
   app.engine('html', require('ejs').renderFile);
   app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/', function (req, res) {
   res.render('login.html', {title: 'Login'});
});
app.get('/index', function (req, res) {
   res.render('index.html', {title: 'Daily'});
});
app.get('/monthly', function (req, res) {
   res.render('monthly.html', {title: 'Monthly'});
});
app.get('/yearly', function (req, res) {
   res.render('yearly.html', {title: 'Login'});
});
app.get('/register', function (req, res) {
   res.render('register.html', {tittle: 'Register'});
});
app.get('/logout', function (req, res) {
   req.logout();
   res.redirect('/');
});

var user="";

io.sockets.on('connection', function(socket) {
    console.log('socket.io started');
    socket.on('login', function(data) {
        console.log('login');
        var checkUser= db.users.find(data);
        console.log(checkUser);
        if(checkUser !== null) {
            console.log('checked user');
            user=checkUser.username;
            app.get('/index', function(req, res) {
                res.direct('/index');
            });
            socket.emit('uploadList', checkUser.total);
        }
    });
    socket.on('purchaseLog', function(data) {
        var usercollection = db.users.find({username: user});
        usercollection.purchase.save(data);
    });
    socket.on('depositLog', function(data) {
        var usercollection = db.users.find({username: user});
        usercollection.deposit.save(data);
    });
    socket.on('goLogout', function(data){
        app.get('/logout', function (req, res) {
            req.logout();
            res.redirect('/');
        });
    });
    socket.on('goIndex', function(data){
        app.get('/index', function (req, res) {
            res.redirect('/index');
        });
    });
    socket.on('goMonthly', function(data){
        app.get('/monthly', function (req, res) {
            res.redirect('/monthly');
        });
        var monTot = db.users.find({username: user}).monthlyTotal;
        socket.emit('wentMonthly', monTot);
    });
    socket.on('goYearly', function(data){
        app.get('/index', function (req, res) {
            res.redirect('/index');
        });
        var yearTot = db.users.find({username: user}).yearlyTotal;
        socket.emit('wentYearly', yearTot);
    });
    socket.on('registered', function(data){
        db.users.save(data);
        app.get('/index', function (req, res) {
            res.redirect('/index');
        });
    });
});

server.listen(3000);
Problem courtesy of: Christian Grabowski

Solution

On the client you might have forgotten to add the socket.io lib:

On the server it should be working (you defined io correctly).

Solution courtesy of: RoryKoehein

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Why is io undefined?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×