koa-socketio-session
v1.0.0
Published
A socket.io middleware to share session with Koa app.
Downloads
144
Readme
koa-socketio-session
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
const koaSocketioSession = require('koa-socketio-session').HandleKoaSession;
koaSocketioSession(app, opt)
- opt, see koa-session options,
store
is required in opt
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
const koaSocketioSession = require('koa-socketio-session').HandleKoaGenericSession;
koaSocketioSession(app, opt)
- opt, see koa-generic-session options,
store
is required in opt
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
const koaSocketioSession = require('koa-socketio-session').HandleKoaSession2;
koaSocketioSession(app, store, key)
- opt, see koa-session2 options,
store
is required in opt
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