https://github.com/peerigon/socket.io-session-middleware
share connect/express sessions with socket.io
https://github.com/peerigon/socket.io-session-middleware
Last synced: 11 months ago
JSON representation
share connect/express sessions with socket.io
- Host: GitHub
- URL: https://github.com/peerigon/socket.io-session-middleware
- Owner: peerigon
- License: mit
- Created: 2014-06-11T17:14:26.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2020-08-27T12:39:00.000Z (almost 6 years ago)
- Last Synced: 2024-10-31T17:58:30.536Z (over 1 year ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 18
- Watchers: 17
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## socket.io-session-middleware
Share connect/express sessions with socket.io 1.x
## Setup
[](https://npmjs.org/package/socket.io-session-middleware)
[](https://david-dm.org/peerigon/socket.io-session-middleware)
## Example
**Server**
```javascript
var socketSession = require("socket.io-session-middleware");
var session = {
store: new connect.session.MemoryStore(),
secret: "secret",
key: "mykey.sid",
cookieParser: connect.cookieParser("secret"),
};
io.use(socketSession(session));
io.on("connection", function (socket) {
socket.on("whoAreYou", function (callback) {
//read from session
callback(socket.session.name);
});
socket.on("setName", function (data) {
//write to session
socket.session.name = data.name;
});
});
```
**Client**
```javascript
socket.emit("setName", { name: "hans" });
socket.emit("whoAreYou", function (name) {
console.log("I am " + name);
});
//=> I am hans
```
A full featured example can be found in the _example_ folder.
## Notes
Make sure to fire a http request to initialize the session/cookie before accessing the session with socket.io.
If you are serving the socket.io client with your node.js server this won't be a problem for you.
## Sponsors
[
](https://peerigon.com)