npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

session.io

v1.0.0

Published

Gets session data for socket.io

Downloads

9

Readme

#session.io

I had been using session.socket.io with quite a bit of success. The problem with this module and many others is that when getting session data, the call is asynchronous. This caused problems when trying to get session data for other sockets besides the one that was currently being used. i.e. io.sockets.forEach(function(_socket){...});

So I needed to get session data during authorization like I was doing previously. I wrote my piece of code basing it off of session.socket.io. Because it was such a re-write I just decided to create a new module for it. After writing my module I found socket.io-session. I looked at their code and saw some things that I liked in it and adapted it to my code (i.e. allowing a callback to continue authorization).

##Quick Start ###Installation

npm install session.io

###Setup This script is under /examples/test.js if you want to test it.

var express = require('express');
var app = express();
var server = require('http').createServer(app);

//Setup cookie and session handlers
//Note: for sessionStore you can use any sessionStore module that has the .load() function
//but I personally use the module 'sessionstore' to handle my sessionStores.
var cookieParser = express.cookieParser('secret');
var sessionStore = require('sessionstore').createSessionStore();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  //...truncate...//
  app.use(cookieParser);
  //make sure to use the same secret as you specified in your cookieParser
  app.use(express.session({secret: 'secret', store: sessionStore}));
  app.use(app.router);
});

app.get('/', function(req, res){
  res.send('<script src="/socket.io/socket.io.js"></script><script>io.connect();</script>Connected');
});

server.listen(app.get('port'), function(){
  console.log('Listening on port ' + app.get('port'));
});

var io = require('socket.io').listen(server);

io.configure(function(){
  //use session.io to get our session data
  io.set('authorization', require('session.io')(cookieParser, sessionStore));
});

io.on('connection', function(socket){
  //we now have access to our session data like so
  var session = socket.handshake.session;
  console.log(session);
});

##Specifics When you call require('session.io'), it returns a function. You may be thinking, "So what exactly can I pass to the session.io function?" Well, let me tell you. When you call that function, it takes the following parameters:

  • cookieParser :: required - instance of express.cookieParser()
  • sessionStore :: required - instance of any sessionStore module that has the .load() function
  • key :: optional - the key used in express.session() if you set one
  • fn :: optional - function to call to handle any additional authorization you may want to do - session will be set here