1

I'm trying to check the username in the NodeJs chat with the user session. Is there a secure way to do it? (My NodeJs server isn't in the same place as the host of the website). I'm using this code NodeJs + express to do the chat application.

Code:

var express = require('express');
var app = express(), 
    server = require('http').createServer(app),
    io = require('socket.io').listen(server, { log: false });

server.listen(8080);

app.get('/chat.js', function(req, res) {
    res.sendfile('chat.js');
});

/* CHAT FUNCTIONS */

io.sockets.on('connection', function (socket) {
    socket.on('send', function (data) {
        // I wanna get the user session
        // something like this:
        // (code below does not exist)

        var username = session['username'];

        console.log(username + " is here.");
        io.sockets.emit('message', data);
    });

});

How can I do this?

Krease
  • 15,805
  • 8
  • 54
  • 86
  • You really don't want to be sending session data across the internet *especially with the username* without tunneling it through an encrypted protocol (e.g. HTTPS). – Alex W Nov 20 '13 at 17:52
  • Have you looked at [this answer](http://stackoverflow.com/a/4755045/2119660) about session storage w/ socket.io? – Jeff Sisson Nov 21 '13 at 04:32

1 Answers1

0

You need to use common storage for sessions e.g. memcached or redis. Than u will have common access.

YarikKotsur
  • 136
  • 1
  • 4