Sunday, 28 May 2017

Node.js / Socket.io newbie got some questions

I'm currently trying to create a little web-game for training purposes. Never really dealt with node.js and socket.io!

So I started by coding the website itself using node.js express and a mongoose database to store user accounts and their values.

Now I arrived the point, where the user gets logged in and gets redirected to the main site and I'm pretty confused on how to continue!

Do I have to create the socket now to have real time updates on the user account values, such as coins?

Screenshot of the values in the dashboard which should always be updated once the user reduces the amount: image of the coins

The right side, for example shows values which can update. And I want the values on the right side to be always the exact value stored in the database.

Do I have to create the socket now to manage this?

My current javascript code to render the site, after the user authenticated himself and got redirected:

var express = require('express');
var router = express.Router();

// Get Site
router.get('/dashboard', ensureAuthenticated, function(req, res){
    res.render('dashboard', {username: req.user.username});
});

function ensureAuthenticated(req, res, next){
    if(req.isAuthenticated())
    {
        return next();
    }
    else
    {
        req.flash('error_msg', 'You are not logged in');
        res.redirect('/users/login');
    }
}

module.exports = router;

Do I use the session or the username itself now to build the websocket?

Sorry for all this questions, but as mentioned, I've never worked with node.js and socket.io. Literally just been coding in PHP, JS and C#.

I'm thankful for every suggestion / help



via turel

No comments:

Post a Comment