Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session Store and Socket.IO #213

Closed
tomasbonco opened this issue May 25, 2014 · 3 comments
Closed

Session Store and Socket.IO #213

tomasbonco opened this issue May 25, 2014 · 3 comments

Comments

@tomasbonco
Copy link

Hello,

I'm trying hard to make Socket.IO working under Kraken. My problem is to get sessionStore to compare sessions.

First, I would like to say, I consider this as architecture gap, because what I want to do is to start Socket.IO at the moment as Kraken and I want to make them connected. The problem is that Kraken allows me to use middleware only after request was made, without providing any API for me otherwise. This part is also undocumented.

From the start: Solution based on advice #39 creates Sockets after application is created and allows them to listen under same port.

Then I want to do some auhorization, just like I saw (importand line is 72):
https://github.com/leeroybrun/socketio-express-sessions/blob/master/server.js

As you can see SessionStore was created in like 16, passed to express on line 26, and used on 72. Problem is that Kraken is using it's configurable files to handle that. Actually it's not a problem, I like that idea. Problem is that there is some "module": "memory" as default (config/middleware.json), and I have no idea what that means. It's not documented, not in examples, I have't found in code. And I need to use it. Best part is, that there is no module memory, what makes me think memory is not a real value. But sessions are stored somewhere. So how can I access them? Or If my way of thinking is wrong, how to do it correctly?

Thank you,
Tomáš

@totherik
Copy link
Member

totherik commented Jun 2, 2014

This example pertains to kraken 1.0.x. I was able to get this working with the following code. It's slightly ugly due to having to know express private members (app._router.stack), but IMO cleaner than the example posted above b/c you don't need to manually deal with cookies, session stores, etc.

// index.js
'use strict';

var http = require('http');
var io = require('socket.io');
var express = require('express');
var kraken = require('kraken-js');


var app, server, realtime;

function middleware(event) {
    var stack = event.app._router.stack;
    middleware[event.config.name] = stack[stack.length - 1].handle;
}


app = express();
app.use(kraken());
app.on('middleware:after:cookieParser', middleware);
app.on('middleware:after:session', middleware);


server = http.createServer(app);
server.listen(8000);

realtime = io(server);
realtime.set('authorization', function (data, callback) {
    var req, res;

    // Shim request and response.
    req = {
        originalUrl: data.originalUrl || data.url,
        headers: data.headers,
        secret: data.secret,
        cookies: null,
        signedCookies: null,
        secureCookies: null
    };
    res = {};

    middleware.cookieParser(req, res, function (err) {
        if (err) {
            callback(err, false);
            return;
        }

        middleware.session(req, res, function (err) {
            if (err) {
                callback(err, false);
                return;
            }

            if (!req.session) {
                callback(new Error('Session not found.'), false);
                return;
            }

            if (!req.session.auth) {
                callback(new Error('User not authenticated.'), false);
                return;
            }

            data.session = req.session;
            callback(null, true);
        });
    });
});

realtime.on('connection', function (socket) {
    var timer;

    timer = setInterval(function () {
        socket.emit('ping', Date.now());
    }, 1000);

    socket.on('pong', console.log.bind(null, 'pong'));
    socket.on('disconnect', function() {
        clearInterval(timer);
    });
});
// routes.js
'use strict';


module.exports = function (router) {

    router.get('/', function (req, res) {
        res.setHeader('content-type', 'text/html');
        res.end('<script src="/socket.io/socket.io.js"></script> \
                <script> \
                var socket = io.connect("http://localhost"); \
                socket.on("ping", function (data) { \
                    console.log("ping", data); \
                    socket.emit("pong", Date.now()); \
                }); \
                </script>');
    });

    router.get('/login', function (req, res) {
        req.session.auth = true;
        res.redirect('/');
    });

    router.get('/logout', function (req, res) {
        req.session.auth = false;
        res.redirect('/');
    });

};

Re: the memory store, it's implemented in dev mode and is the default store used by express. For production use, other datastores (such as redis, postgres, etc) are recommended.

@totherik totherik closed this as completed Jun 2, 2014
@tomasbonco
Copy link
Author

Thank you!

@tomalex0
Copy link
Contributor

Do you have session authorization solution for krakenjs 0.7.x ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants