Wednesday, 6 February 2019

Socket.io connecting many times instead of just once

I have build this app, with express.js, its a basic webapp, but now i want to add a simple messaging system.I already had built my express app and server like this:const app = require('./app'); app.set('port', process.env.PORT || 7777); const server = app.listen(app.get('port'), () => { console.log(`Express running → PORT ${server.address().port}`); But as I follow this tutorial on socket.io: https://socket.io/get-started/chat/I changed my start.js to this, to use socket.io:// Start our app! const app = require('./app'); app.set('port', process.env.PORT || 7777); const server = app.listen(app.get('port'), () => { console.log(`Express running → PORT ${server.address().port}`); }); // const http = require('http').Server(app); const io = require('socket.io').listen(server); io.on('connection', (socket) => { console.log('a user connected'); }); It says on the tutorial that when a user signs up i should see a console log saying: a user is connectedHowever I get one log per frame... not just one per connected user.​Is this behaviour correct with the changes i made.. or should i still be getting still just one log per connected user?​Im also using pug as my templating language, and i have, at the end of my layout file this: block scripts script(src=`https://maps.googleapis.com/maps/api/js?key=${process.env.MAP_KEY}&libraries=places`) script(src="/dist/App.bundle.js") script(src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js") script. const socket = io() App.js:const express = require('express'); const session = require('express-session'); const mongoose = require('mongoose'); const MongoStore = require('connect-mongo')(session); const path = require('path'); const cookieParser = require('cookie-parser'); const bodyParser = require('body-parser'); const passport = require('passport'); const promisify = require('es6-promisify'); const flash = require('connect-flash'); const expressValidator = require('express-validator'); const routes = require('./routes/index'); const helpers = require('./helpers'); const errorHandlers = require('./handlers/errorHandlers'); require('./handlers/passport'); // create our Express app const app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); // this is the folder where we keep our pug files app.set('view engine', 'pug'); // we use the engine pug, mustache or EJS work great too // serves up static files from the public folder. Anything in public/ will just be served up as the file it is app.use(express.static(path.join(__dirname, 'public'))); // Takes the raw requests and turns them into usable properties on req.body app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // Exposes a bunch of methods for validating data. Used heavily on userController.validateRegister app.use(expressValidator()); // populates req.cookies with any cookies that came along with the request app.use(cookieParser()); // Sessions allow us to store data on visitors from request to request // This keeps users logged in and allows us to send flash messages app.use(session({ secret: process.env.SECRET, key: process.env.KEY, resave: false, saveUninitialized: false, store: new MongoStore({ mongooseConnection: mongoose.connection }) })); // // Passport JS is what we use to handle our logins app.use(passport.initialize()); app.use(passport.session()); // // The flash middleware let's us use req.flash('error', 'Shit!'), which will then pass that message to the next page the user requests app.use(flash()); // pass variables to our templates + all requests app.use((req, res, next) => { res.locals.h = helpers; res.locals.flashes = req.flash(); res.locals.user = req.user || null; res.locals.currentPath = req.path; next(); }); // promisify some callback based APIs app.use((req, res, next) => { req.login = promisify(req.login, req); next(); }); // After allllll that above middleware, we finally handle our own routes! app.use('/', routes); // If that above routes didnt work, we 404 them and forward to error handler app.use(errorHandlers.notFound); // One of our error handlers will see if these errors are just validation errors app.use(errorHandlers.flashValidationErrors); // Otherwise this was a really bad error we didn't expect! Shoot eh if (app.get('env') === 'development') { /* Development Error Handler - Prints stack trace */ app.use(errorHandlers.developmentErrors); } // production error handler app.use(errorHandlers.productionErrors); // done! we export it so we can start the site in start.js module.exports = app;

Submitted February 06, 2019 at 03:32PM by giorgiomartini

No comments:

Post a Comment