Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-socket.io-session

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-socket.io-session

Share a cookie-based express-session middleware with socket.io

  • 1.2.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8K
decreased by-0.48%
Maintainers
1
Weekly downloads
 
Created
Source

express-socket.io-session

Share a cookie-based express-session middleware with socket.io

express-socket.io-session works with express > 4.0.0 and socket.io > 1.0.0 and won't be backward compatible.

Please help me notice errors or ask me for improvements creating an issue.

Overview

On every socket connection, you'll have socket.handshake.session pointing to the same req.session you would expect in any express app handler that uses the express-session middleware module.

Installation

$ npm install express-socket.io-session

Usage

var session require("express-session")({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true
});
// Use sessions with express
app.use(session);
//Share the express' session with socket.io
io.use(require("express-socket.io-session")(session));

Sharing session data with a namespaced socket

io.of('/namespace').use(sharedsession(session));

Using your own custom cookie-parser instance

...
var cookieParser = require("cookie-parser");
...
io.use(require("express-socket.io-session")(session, cookieParser({/* your params to cookie-parser* /})));

Example

# Install express, socket.io and express-session 
$ npm install express socket.io express-session 
# Install this module
$ npm install express-socket.io-session

index.js

var app = require('express')(),
  server  = require("http").createServer(app),
  io = require("socket.io")(server),
  session = require("express-session")({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true
  }),
  sharedsession = require("express-socket.io-session");


// Attach session
app.use(session);

// Share session with io sockets

io.use(sharedsession(session));

server.listen(3000);

API

This module exports a socket.io's middleware for using with io.use() You get a shared session middleware by requiring the module.

require("express-socket.io-session")( express_session_middleware, [cookieparser_instance] )

  • express_session_middleware is required and must be an express middleware function created with the express-session module that allows cookie-based sessions.
  • cookieparser_instance is optional. If you don't provide en instance created by cookie-parser, this module creates one for you with defaults.

Inspiration

Although there are a couple of modules that allow to share session objects between express and socket.io, I wanted to be able to share the modules without affecting regular express-session instantiation.

These modules do the same work but with different approachs on initialization.

License

The MIT License (MIT)

Copyright (c) 2014-2015 osk <oscar@shovelapps.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 18 Apr 2015

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc