socket.io-cookie-parser

socket.io middleware for parsing cookies.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
socket.io-cookie-parser
1001.0.09 years ago9 years agoMinified + gzip package size for socket.io-cookie-parser in KB

Readme

socket.io-cookie-parser
This package provides a very simple cookie parser middleware for your socket.io application. This makes it easy to share cookies between socket.io and express, for example.

Installation

Install the package as an application dependency.
npm install socket.io-cookie-parser --save

Usage

Inject the middleware into your io stack prior to authorization or request handling.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.use(cookieParser());
io.use(authorization);

function authorization(socket, next) {
  // cookies are available in:
  // 1. socket.request.cookies
  // 2. socket.request.signedCookies (if using a secret)
});
If you need to use a secret or additional parsing options, this middleware acts as an extremely thin wrapper for express cookie-parser, so pass arguments through exactly as you would when using that. For example, using a secret and custom decoding mechanism could be done with the following:
io.use(cookieParser('keyboard cat', {
  decode: function (str) {
    return str.replace('-', '_');
  }
}));

License

socket.io-cookie-parser is released under the MIT License.