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 - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

17

example/index.js

@@ -17,11 +17,16 @@ var debug = require("debug")("express-socket.io-session:example"),

// Share session with io sockets
io.use(sharedsession(session));
//io.use(sharedsession(session));
//app.use("*", function(req, res,next) {
// debug("Session data is %j.", req.session );
// next();
//});
app.use("*", function(req, res, next) {
debug("Express `req.session` data is %j.", req.session);
next();
});
app.use("/", require("express").static("./"));
io.use(function(socket, next) {
debug("socket.handshake session data is %j.", socket.handshake.session);
next();
});
app.use(require("express").static(__dirname));
// Set session data via express request

@@ -28,0 +33,0 @@ app.use("/login", function(req, res, next) {

@@ -1,18 +0,3 @@

var cookieParser = require('cookie-parser')();
var cookieparser = require('cookie-parser');
var debug = require("debug")("express-socket.io-session");
/*
* Inspiration
* ============
*
* [socket.io and express 4 sessions]
* http://stackoverflow.com/questions/23494016/socket-io-and-express-4-sessions
*
* [Socket.io 1.0.5 : How to save session variables?]
* http://stackoverflow.com/questions/24290699/socket-io-1-0-5-how-to-save-session-variables/24380110#24380110
*
* sesion.socket.io
* ----------------
* I don't like this one because you can not reuse
* https://www.npmjs.org/package/session.socket.io
*/

@@ -25,11 +10,20 @@ // The express session object will be set

*
* @param {Function} an express-session middleware function
* @param {Function} an express-session middleware function to reuse with io.use
* @param {Function} an express-session middleware function to reuse with express-session
*/
module.exports = function(session) {
module.exports = function(expressSessionMiddleware, cookieParserMiddleware) {
var cookieParserMiddleware,
socketIoSharedSessionMiddleware;
if (typeof cookieParserMiddleware === 'undefined') {
debug("No cookie-parser instance passed as argument");
debug("Creating a cookie-parser instance with default values")
cookieParserMiddleware = cookieparser();
}
debug("Creating socket.io middleware");
return function(socket, next) {
var socketIoSharedSessionMiddleware = function(socket, next) {
var req = socket.handshake;
var res = {};
//Parse session cookie
cookieParser(req, res, function(err) {
cookieParserMiddleware(req, res, function(err) {
if (err) {

@@ -39,3 +33,3 @@ debug("cookieParser errored");

}
session(req, res, function(req, res) {
expressSessionMiddleware(req, res, function(req, res) {
next();

@@ -45,2 +39,3 @@ });

};
};
return socketIoSharedSessionMiddleware;
};
{
"name": "express-socket.io-session",
"version": "1.1.1",
"version": "1.2.0",
"description": "Share a cookie-based express-session middleware with socket.io",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -6,6 +6,4 @@ express-socket.io-session

**Notice**
**THIS MODULE WORKS FINE BUT IS EXPERIMENTAL.**
**THIS MODULE IS EXPERIMENTAL. IT WORKS. BUT IT WON'T WORK *AS EXPECTED* YET BECAUSE I STILL DON'T KNOW WHAT TO EXPECT FROM IT.**
**It works with express > 4.0.0 and socket.io > 1.0.0 and won't be backward compatible.**.

@@ -25,11 +23,11 @@

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));
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));

@@ -41,4 +39,12 @@

## Usage example
**Using your own custom [cookie-parser](https://www.npmjs.com/package/cookie-parser) instance**
...
var cookieParser = require("cookie-parser");
...
io.use(require("express-socket.io-session")(session, cookieParser({/* your params to cookie-parser* /})));
## Example
```

@@ -53,26 +59,26 @@ # Install express, socket.io and express-session

```
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");
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
// Attach session
app.use(session);
io.use(sharedsession(session));
// Share session with io sockets
server.listen(3000);
io.use(sharedsession(session));
```
server.listen(3000);
## API

@@ -83,6 +89,21 @@

**require("express-socket.io-session")( express_session_middleware )**
**require("express-socket.io-session")( express_session_middleware, [cookieparser_instance] )**
The `express_session_middleware` parameter is **mandatory** and must be an express middleware function created with the [express-session](https://www.npmjs.org/package/express-session) module that allows cookie-based sessions.
* `express_session_middleware` is **required** and must be an express middleware function created with the [express-session](https://www.npmjs.org/package/express-session) module that allows cookie-based sessions.
* `cookieparser_instance` is optional. If you dont provide en instance created by `require("cookie-parser")()`, this module creates one for you with defaults.
##Inspiration
* [socket.io and express 4 sessions](http://stackoverflow.com/questions/23494016/socket-io-and-express-4-sessions)
* [Socket.io 1.0.5 : How to save session variables?](http://stackoverflow.com/questions/24290699/socket-io-1-0-5-how-to-save-session-variables/24380110#24380110)
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.
* [session.socket.io](https://www.npmjs.org/package/session.socket.io)
* [socket.io-session-middleware](https://github.com/peerigon/socket.io-session-middleware)
#License

@@ -89,0 +110,0 @@

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