koa-socketio-cookie-helper
v1.1.1
Published
koa.js + socket.io cookie helper
Downloads
2
Maintainers
Readme
#koa-socketio-cookie-helper
koa.js + socket.io helper for accessing cookies from a WebSocket connection
WebSocket
This is a fork of anilanar/koa-ws-cookie-helper and for ws
module should be used original library
Installation
$ npm install koa-socketio-cookie-helper
Example
var app = require('koa')();
var server = require('http').createServer(app.callback());
var cookieHelper = require('koa-ws-cookie-helper');
app.keys = ['secret'];
var io = require('socket.io')(server);
io.on('connection', function(){
});
io.use(function (socket, next) {
// tries to get value of cookie named 'foo'
// e.g. for 'foo=bar', returns 'bar'
var cookieValue = cookieHelper.get(socket, 'sid', app.keys);
// if you use unsigned cookies or
// don't care if the cookie is legit or not
// omit the keys parameter
var cookieValue = cookieHelper.get(socket, 'sid');
});
server.listen(3000);
How does it work?
Browsers put any cookies from the same host into initial Socket.io handshake headers. This lets typical HTTP-session mechanisms to work with Socket.io. koa.js makes use of Cookies module to handle cookie management. Signed cookies are signed and verified using Keygrip. This module combines methods used by them to parse cookies in ws.handshake.headers.cookie
.
API
There is only a single public function:
###get(ws, name, [keys])
Returns value of the cookie named name
, using keyset keys
. Cookie is searched in 'Cookie'
entry of handshake headers. If no cookie could be found or if the signed cookie does not match with any key in the keyset, returns undefined
.
License
MIT