express-socket.io-session
Advanced tools
Comparing version 1.3.2 to 1.3.3
{ | ||
"name": "express-socket.io-session", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Share a cookie-based express-session middleware with socket.io", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
128
README.md
@@ -30,42 +30,47 @@ express-socket.io-session | ||
io.use(sharedsession(session, { | ||
autoSave:true | ||
})); | ||
```js | ||
io.use(sharedsession(session, { | ||
autoSave:true | ||
})); | ||
``` | ||
## Usage | ||
```js | ||
var session = require("express-session")({ | ||
secret: "my-secret", | ||
resave: true, | ||
saveUninitialized: true | ||
}); | ||
var sharedsession = require("express-socket.io-session"); | ||
## Usage | ||
// Use express-session middleware for express | ||
app.use(session); | ||
var session = require("express-session")({ | ||
secret: "my-secret", | ||
resave: true, | ||
saveUninitialized: true | ||
}); | ||
var sharedsession = require("express-socket.io-session"); | ||
// Use express-session middleware for express | ||
app.use(session); | ||
// Use shared session middleware for socket.io | ||
// setting autoSave:true | ||
io.use(sharedsession(session, { | ||
autoSave:true | ||
})); | ||
// Use shared session middleware for socket.io | ||
// setting autoSave:true | ||
io.use(sharedsession(session, { | ||
autoSave:true | ||
})); | ||
``` | ||
**Sharing session data with a namespaced socket** | ||
io.of('/namespace').use(sharedsession(session, { | ||
autoSave: true | ||
})); | ||
```js | ||
io.of('/namespace').use(sharedsession(session, { | ||
autoSave: true | ||
})); | ||
``` | ||
**Using your own custom [cookie-parser](https://www.npmjs.com/package/cookie-parser) instance** | ||
... | ||
var cookieParser = require("cookie-parser"); | ||
... | ||
io.use(sharedsession(session, cookieParser({ | ||
/* your params to cookie-parser* / | ||
})); | ||
```js | ||
... | ||
var cookieParser = require("cookie-parser"); | ||
... | ||
io.use(sharedsession(session, cookieParser({ | ||
/* your params to cookie-parser* / | ||
})); | ||
``` | ||
## Example | ||
@@ -79,43 +84,46 @@ | ||
```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"); | ||
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); | ||
// Attach session | ||
app.use(session); | ||
// Share session with io sockets | ||
// Share session with io sockets | ||
io.use(sharedsession(session)); | ||
io.use(sharedsession(session)); | ||
io.on("connection", function(socket) { | ||
// Accept a login event with user's data | ||
socket.on("login", function(userdata) { | ||
socket.handshake.session.userdata = userdata; | ||
io.on("connection", function(socket) { | ||
// Accept a login event with user's data | ||
socket.on("login", function(userdata) { | ||
socket.handshake.session.userdata = userdata; | ||
socket.handshake.session.save(); | ||
}); | ||
socket.on("logout", function(userdata) { | ||
if (socket.handshake.session.userdata) { | ||
delete socket.handshake.session.userdata; | ||
socket.handshake.session.save(); | ||
}); | ||
socket.on("logout", function(userdata) { | ||
if (socket.handshake.session.userdata) { | ||
delete socket.handshake.session.userdata; | ||
socket.handshake.session.save(); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
}); | ||
server.listen(3000); | ||
server.listen(3000); | ||
``` | ||
## API | ||
var sharedsession = require("express-socket.io-session"); | ||
io.use(sharedsession(express_session)); | ||
```js | ||
var sharedsession = require("express-socket.io-session"); | ||
io.use(sharedsession(express_session)); | ||
``` | ||
###sharedsession( express_session, [cookieparser], [options]) | ||
### sharedsession( express_session, [cookieparser], [options]) | ||
@@ -122,0 +130,0 @@ * **express_session** - This parameter 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 over Express. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172
17008
9