socket.io doesn't store session - node.js

I have application which use node.js and socket.io.
I would like store some information in session. I made example. But my code doesn't work. When I run my script and refresh page I see:
NaN
2
Next, when I refresh page again I can see
NaN
2
NaN
2
So session is not stored. How can I fix my code ?
var Session = require('express-session'),
SessionStore = require('session-file-store')(Session);
var session = Session({ secret: 'pass', resave: true, saveUninitialized: true });
var config = require("./config.json");
var express = require('express');
var app = express();
var socketio = require('socket.io');
var server = app.listen(3000, function(){
console.log('Start')
});
var io = require('socket.io').listen(server);
app.use(session); // session support
app.get('/', function (req, res) {
req.session.ziom = 1;
req.session.save();
console.log('dec');
});
var http = require('http');
var ios = require('socket.io-express-session');
io.use(ios(session));
io.sockets.on('connection', function(socket){
console.log(parseInt(socket.handshake.session.test));
socket.handshake.session.test =2;
socket.handshake.session.save();
console.log(parseInt(socket.handshake.session.test));
});

I dont't understand this error because I copy-past your code and everything* work fine !
*I can't see all your code/project, I don't know if you have an index.html file who start connexion event with the node server. I had this file with minimum code for test.
Change in my app.js is res.sendfile('./index.html');
See all my code below for comparison.
app.js
var Session = require('express-session'),
SessionStore = require('session-file-store')(Session);
var session = Session({ secret: 'pass', resave: true, saveUninitialized: true });
var express = require('express');
var app = express();
var socketio = require('socket.io');
var server = app.listen(3000, function(){
console.log('Start')
});
var io = require('socket.io').listen(server);
app.use(session); // session support
app.get('/', function (req, res) {
req.session.ziom = 1;
req.session.save();
console.log('dec');
res.sendfile('./index.html');
});
var http = require('http');
var ios = require('socket.io-express-session');
io.use(ios(session));
io.sockets.on('connection', function(socket){
console.log(parseInt(socket.handshake.session.test));
socket.handshake.session.test =2;
socket.handshake.session.save();
console.log(parseInt(socket.handshake.session.test));
});
index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
Sincerely hope that helps.

I assume you're trying to open the socket.io connection using an HTML file similar to the bootstrap file in the tutorial (second html snippet after this link)?
With a small change, your code worked for me, where working means you get
NaN
2
2
2
on the console with two page refreshes.
In order to get this, I created the boostrap file relative to the index.js file (a copy of your code above) at views/socket.html, and ensured this was served by the express app by adding these lines to the handler for the '/' route:
require('fs').readFile('views/socket.html', function (err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data, "utf8");
res.end();
});
Then visited http://localhost:3000 twice.
It is required for this file to be served by the express app in order for the session cookie to be stored; so that the socket.io connection knows to use the same cookie to be linked to the same session at the next refresh.

Related

Troubles with express and socket.io sessions

I seem to not be able to share sessions between express and socket io . It works when on localhost but not when on my server. There is something I am missing.
The data I store in socket.handshake.session ('age' here) is not saved on the express side.
My app.js goes like this:
var cookieParser = require('cookie-parser');
var session = require('express-session');
var app = express();
app.use(cookieParser()); // read cookies (needed for auth)
var sessionMiddleware = session({
secret: 'keyboard cat',
resave: true,
name: 'sessionId',
saveUninitialized: true,
cookie: { maxAge: 60000 },
// store: new redisStore({ host: 'localhost', port: 6379, client: redisClient, ttl: 86400 }),
});
app.use(sessionMiddleware);
app.set("sessionMW", sessionMiddleware);
module.exports = app;
My bin\www goes like this:
var app = require('../app');
var debug = require('debug')('testserver:server');
var http = require('http');
var sharedsession = require("express-socket.io-session");
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var session = app.get("sessionMW")
io.use(sharedsession(session, {autosave:true}));
server.listen(port);
I then have a serverEvent.js that goes like this:
var serverEvents = module.exports = function(io){
io.sockets.on('connection', function (socket) {
...
...
...
socket.on('updateAgeSession', function (message) {
socket.handshake.session.age = "18";
socket.handshake.session.save();
console.log(socket.handshake.session);
})
})
}
And finaly I have this index.js for my routes:
module.exports = function (app, passport) {
// show the home page (will also have our login links)
app.get('/', function (req, res, next) {
res.cookie('mycookies', 'express');
res.cookie('age', req.session.age);
cookie1= req.cookies;
console.log(req.session);
console.log(req.sessionID);
console.log(req.sessionStore.sessions);
if (req.session.views) {
req.session.views++;
console.log(req.session.views);
} else {
req.session.views = 1
console.log(req.session.views);
}
if(req.session.age){
console.log("OOOOOOOOOOOOOOOOOO");
} else {
console.log("XXXXXXXXXXXXXXXXXXXXXX");
}
res.render('homePage'); // ejs template
});
}
So my problem is that after I enter that Im over 18 when I refresh the page , the req.session.age will be undefined, but only when my code runs on server, it does work on localhost. I dont get it.
Please help
For anyone that might have got the same problem I had, here is the solution :
I also have a client side code that goes like this :
var socket = io.connect("http://ip:3000");
var globalVar = [];
// Sent on connection/searchValidatedFromClient by server
socket.on("loadHomePageFromServer", function(message) {
...}
Problem was io.connect("http://ip:3000");.
By changing it to io.connect("http://namesite.com"); or io.connect("http://ip:80");, it works!

Executing a Node.js script to send a Twilio message from a client side button click

I am looking to have a button on my web application query a MongoDB and send out a text message to the appropriate individuals based on the result of that query.
The issue I am having is in testing. I can't figure out a way to have the button on the client side run the node.js file on the server side.
Node.js: v8.9.4
Vue.js: 2.5.13
Express: 4.13.3
EDIT:
I've been able to get the nodejs script to run on its own. I just don't know how to call it from the client side
EDIT 2:
Main.js
// Dependencies
var http = require('http');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var busboy = require('connect-busboy');
var cors = require('cors');
var mongoose = require('mongoose');
// Configuration
var config = require('./config');
var twilio = require('twilio');
// Database
mongoose.connect(config.database);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
console.log('Connected to database');
});
var app = express();
app.set('port', process.env.PORT || 3000);
// Setup middleware
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.sessionSecret));
app.use(express.static(path.join(__dirname, 'dist')));
app.use(busboy());
app.use(cors({
origin: true,
credentials: true
}));
app.get('/session',function(req){
twilio.notifyOnSession();
});
var server = http.createServer(app);
var port = app.get('port');
server.listen(port);
console.log('Listening on port ' + port);
// Load server router
require('./router')(app);
EDIT 3:
Dashboard.vue
Here is the code that is already in place to redirect to the correct part of the app
getHelpNext() {
var topic = this.pickedTopic;
topic = topic.toLowerCase();
var linkName = '/session/' + topic;
this.$router.push(linkName);
}
EDIT 4:
Router.js
var path = require('path');
module.exports = function(app){
console.log('Initializing server routing');
require('./auth')(app);
require('./api')(app);
// Determine if incoming request is a static asset
var isStaticReq = function(req){
return ['/auth', '/api', '/js', '/css'].some(function(whitelist){
return req.url.substr(0, whitelist.length) === whitelist;
});
};
// Single page app routing
app.use(function(req, res, next){
if (isStaticReq(req)){
return next();
}
res.sendFile(path.join(__dirname, '../dist/index.html'));
});
};
You need to make an endpoint that will get the request. This goes in express:
app.post('/some/end/point', (req, res) => {
// -> send twillo message code here. If you use bodyParser,
// req.body.message holds 'foo bar'
res.status(200).end(); // do this if sending the message is a success
})
where app is your express server that listens on some port.
And this should go in your vue
this.$http.post('/some/end/point', {
message: 'foo bar'
})
You are describing here several components that should be unit tested separately.
Query Mongodb
Node JS script - with mock of mongodb query result and not.
Twillo text sending.
UI test
Your question is probably referring to the last part but in order to properly implement it, you must break the unit test to these following topics.
As for UI test, there are many tools , my favourite is puppeteer.
You need to create an endpoint on your server using expressjs, expose it to the internet and then call it from your client by doing a request.

NodeJS - Access sessions inside socket

I spent 2 days on accessing sessions (log user in) trough sockets.
I already have users in my mongodb, and i can indeed login/register them without sockets (trough post or get). All i need now is the same for sockets.
I have tried these SO solutions:
socket.io and express 4 sessions
How to share sessions with Socket.IO 1.x and Express 4.x?
socket.io and session?
And spend a lot of time googling. And trying different things.
I typically get undefined errors, or deprecated error, or nothing happens at all without errors.
After following the tutorials above, and doing some tweaks myself, my code went from "hard to read" to "too hard to read, must start from scratch". So i wont put up my code here.
Instead, can someone share their bare-minimum code on what it takes to access the sessions inside sockets? With explanations would would be highly appreciated.
UPDATE
I followed this: How to share sessions with Socket.IO 1.x and Express 4.x?
And i got a few issues. It still does not work. the session is empty. Am i doing something wrong? Full code:
var express = require("express");
var Server = require("http").Server;
var session = require("express-session");
var RedisStore = require("connect-redis")(session);
var SESSION;
var app = express();
var server = Server(app);
var sio = require("socket.io")(server);
var sessionMiddleware = session({
store: new RedisStore(),
secret: "keyboard cat",
resave: true,
saveUninitialized: true
});
sio.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
app.use(sessionMiddleware);
app.get("/*", function(req, res){
SESSION = req.session
req.session.name="THE NAME"; // <<<< SHOULDN'T THIS WORK? ITS UNDEFINED
if(req.path=="/"){
res.sendFile(__dirname+"/index.html");
}else{
res.sendFile(__dirname+req.path);
};
});
sio.sockets.on("connection", function(socket) {
SESSION = socket.request.session
console.log("The SESSION variable on socket connection:");
console.log(SESSION); //<<<<<<<<<<<< UNDEFINED
socket.on("get session status",function(){
socket.emit("session status",{ SESSION:SESSION }); // <<<<< EMPTY OBJECT
})
});
server.listen(80);
The accepted answer at the second link works fine for me:
app.js:
var session = require("express-session");
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var sessionMiddleware = session({
secret: "keyboard cat",
resave: true,
saveUninitialized: true
});
app.use(sessionMiddleware);
app.use(function(req, res) {
req.session.name = "THE NAME";
res.sendFile(__dirname + "/index.html");
});
io.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
io.on("connection", function(socket) {
console.log('socket.io connection');
console.dir(socket.request.session);
// above outputs:
// socket.io connection
// { cookie:
// { path: '/',
// _expires: null,
// originalMaxAge: null,
// httpOnly: true },
// name: 'THE NAME' }
});
server.listen(8000);
index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8000');
</script>
Perhaps there is an issue with timing where the session data isn't saved to Redis yet when the socket.io connection is made? You might try delaying the socket.io connection at least until the page is fully rendered (window.onload).

separate logic from view with route

I'm pretty new to Node and I'm using express.
I'm trying to implement simple file explorer.
enough talking here's some code:
(root dir is /app for this ex.)
my app.js:
var express = require('express');
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var explorer = require('./routes/explorer');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use("/public", express.static(__dirname + '/public'));
app.use('/', explorer);
server.listen(3000,function(){
console.log('Server started on http://localhost:3000');
});
io.on('connection',function(client){
console.log('connection with io established');
});
module.exports.app = app;
module.exports.server = server;
see that I have routing '/' to explorer
and the explorer.js:
var express = require('express');
var router = express.Router();
var fs = require('fs');
router.get('/', function(req, res, next) {
res.render('explorer',{
currentPath : currentPath
});
});
function getUserHome() {
var rawPath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
return rawPath;
}
function clicked(){
console.log(getUserHome());
}
the view explorer.ejs:
<div class="clickable">clickme</div>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
$('.clickable').click(function(){
socket.emit('clicked');
});
</script>
basically i want to catch the "clicked" emit from the view and trigger the clicked function in the explorer.js. in other words i want the view and the explorer.js talk by socket.io, so the view can talk with the file system.
i tried few way and got bunch of error.
thank you so much!
So you've triggered the event 'clicked' on your client side :
// Client side :
var socket = io.connect('http://localhost:3000');
$('.clickable').click(function(){
socket.emit('clicked');
});
Now, you have to catch it on the server side, in the explorer.js file :
// Server side :
// Instanciate your socket.io on the server side
var io = require('socket.io')(server)
// Listen for a client connection :
io.sockets.on('connection', function(socket){
// Inside the connection, you can now declare each function for each event triggered on the client side
socket.on('clicked', function() {
// This is your callback,
//+ the code in this scope will be executed
//+ only when the event 'clicked' is emitted
cliked();
});
});

Update express session through socket.io

I have a Node.js server using express framework with a session system. The program is a simple 1vs1 game. In order to play, a user can create a room and an other user can join it so the game can start. The room ID is stored into the user's session.
If you take a look at the following code, you'll see how I manage to access express sessions in a "socket code". This is great, it works. However, I cannot modify values in the session object.
var express = require('express');
var session = require('express-session');
var sessionMiddleware = session({
secret: 'my secret',
resave: false,
saveUninitialized: false
});
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.use(sessionMiddleware);
io.use(function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
io.on('connection', function(socket) {
// some stuff
socket.on('disconnect', function() {
socket.request.session.room = undefined; // this is not working
});
});
server.listen(8080);
Is there a way to do what I want or should I rethink the way rooms are managed ?
Thank you

Resources