Skip to content

Dongss/koa-socketio-session

Repository files navigation

koa-socketio-session

Build Status Coverage Status Dependency Status

A socket.io middleware to share session from Koa app.

Get koa app session by: let mySession = socket.session;

Require

  • Koa2
  • Node 7.6 or greater for async/await support

Supported Koa session middlewares

Install

npm install koa-socketio-session --save

Usage

koa-session

koa-session

Notice:

If you are using koa-session >= 6.0.0, you must pass key option. koa-session 6.x issue

const koaSocketioSession = require('koa-socketio-session').HandleKoaSession;

koaSocketioSession(app, opt)

example

const Koa = require('koa');
const session = require('koa-session');
const koaSocketioSession = require('koa-socketio-session').HandleKoaSession;
const app = new Koa();

// session store
class Store {
    async get(sid) {
        // ...
    }

    async set(sid =  this.getID(24), session, maxAge) {
        // ...
    }

    destroy(sid) {
        // ...
    }
}

app.keys = ['koa2', 'socketio', 'koa-session'];

const sessionOpt = {
    store: new Store(),
    key: '4lKSd^Qma*3',
};
app.use(session(sessionOpt, app));

const server = app.listen(3000, () => {
    console.log('server listen on 3000');
});

const io = require('socket.io')(server, {
    path: '/websocket',
    cookie: true
});

io.use(koaSocketioSession(app, sessionOpt));

io.use((socket, next) => {
    let s = socket.session;
    if (!s || !s.logined) {
        return next(new Error('unauthorized'));
    }
    return next();
});

io.on('connection', (socket) => {
    let s = socket.session;
    console.log(s);
    socket.use((p, next) => {
        let s = socket.session;
        if (!s || !s.logined) {
            return next(new Error('unauthorized'));
        }
        return next();
    });
});

koa-generic-session

koa-generic-session

const koaSocketioSession = require('koa-socketio-session').HandleKoaGenericSession;

koaSocketioSession(app, opt)

example

const Koa = require('koa');
const session = require('koa-generic-session');
// const Store = require('koa-generic-session/lib/memory_store');
const redisStore = require('koa-redis');
const koaSocketioSession = require('koa-socketio-session').HandleKoaGenericSession;

const app = new Koa();

app.keys = ['koa2', 'socketio', 'koa-generic-session'];

const sessionOpt = {
    store: new redisStore(),
    key: '4lKSd^Qma*3',
};

app.use(session(sessionOpt));

const server = app.listen(3000, () => {
    console.log('server listen on 3000');
});

const io = require('socket.io')(server, {
    path: '/websocket',
    cookie: true
});

io.use(koaSocketioSession(app, sessionOpt));

io.use((socket, next) => {
    let s = socket.session;
    if (!s || !s.logined) {
        return next(new Error('unauthorized'));
    }
    return next();
});

io.on('connection', (socket) => {
    let s = socket.session;
    console.log(s);
    socket.use((p, next) => {
        let s = socket.session;
        if (!s || !s.logined) {
            return next(new Error('unauthorized'));
        }
        return next();
    });
});

koa-session2

koa-session2

const koaSocketioSession = require('koa-socketio-session').HandleKoaSession2;

koaSocketioSession(app, store, key)

example

const Koa = require('koa');
const session = require('koa-session2');
const koaSocketioSession = require('koa-socketio-session').HandleKoaSession2;
const app = new Koa();

// session store
class Store {
    constructor() {
    }

    async get(sid) {
        // ...
    }

    async set(session, { sid =  this.getID(24), maxAge } = {}) {
        // ...
    }

    destroy(sid) {
        // ...
    }
}

app.keys = ['koa2', 'socketio', 'koa-session2'];

const sessionOpt = {
    store: new Store(),
    key: '4lKSd^Qma*3',
};
app.use(session(sessionOpt));

const server = app.listen(3000, () => {
    console.log('server listen on 3000');
});

const io = require('socket.io')(server, {
    path: '/websocket',
    cookie: true
});

io.use(koaSocketioSession(app, sessionOpt));

io.use((socket, next) => {
    let s = socket.session;
    if (!s || !s.logined) {
        return next(new Error('unauthorized'));
    }
    return next();
});

io.on('connection', (socket) => {
    let s = socket.session;
    console.log(s);
    socket.use((p, next) => {
        let s = socket.session;
        if (!s || !s.logined) {
            return next(new Error('unauthorized'));
        }
        return next();
    });
});

Test

npm test

About

A socket.io middleware to share session from Koa app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published