Socket
Book a DemoInstallSign in
Socket

koa-socketio-session

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-socketio-session

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

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
41
7.89%
Maintainers
1
Weekly downloads
 
Created
Source

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

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

Keywords

koa

FAQs

Package last updated on 07 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.