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.3.2 to 1.3.3

2

package.json
{
"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",

@@ -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.

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