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

passport-socketio-redis

v1.0.2

Published

passport.js authenticate socket.io with RedisStore using express.js

Downloads

35

Readme

passport-socketio-redis Library for socket.io 1.x and express.js 4.x

Share user information from a passport.js stored in Connect-Redis with socket.io connection.

Installation

$ npm install passport-socketio-redis

Example usage


// Create http server (Based on express.js)

var express = require('express');
var app = express();
var server = app.listen(config.httpOptions.port, function(){
    console.log('Server is listnening on port %d', server.address().port);
});
var cookieParser = require('cookie-parser');
var redis = require("redis").createClient();
var passport = require('passport');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);

var socketioRedis = require("passport-socketio-redis");


// When configure your session for express use options like this.
app.use(session({
    secret:"secret key", // Keep your secret key
    key:"connect.sid", 
    store:new RedisStore({
        host: 'localhost',
        port: 6379,
        client: redis
    }))
);

// Use passport
app.use(passport.initialize());
app.use(passport.session());

// Initialize socket.io 
var io = require("socket.io")(server);

io.use(socketioRedis.authorize({
    passport:passport,
    cookieParser: cookieParser,
    key:         'connect.sid',       
    secret:      'secret key',    
    store:       new RedisStore({ host: 'localhost', port: 6379, client: redis }),
    success:     authorizeSuccess,  // *optional* callback on success - read more below
    fail:        authorizeFail     // *optional* callback on fail/error - read more below
}));

function authorizeSuccess(data, accept)
{
    console.log('Authorized success');
    accept();
}

function authorizeFail(data, message, error, accept)
{
    if(error)
        accept(new Error(message));
}

passport-socketio-redis - Options

cookieParser [function] required:

You have to provide your cookieParser from cookie-parser: express.cookieParser

store [function] required:

You have to provide connect-redis as a SessionStore

key [string] optional:

Defaults to 'connect.sid'. But you're always better of to be sure and set your own key. Don't forget to also change it in your express.session(): app.use(session({ key: 'your.sid-key' }));

secret [string] optional:

As with key, also the secret you provide is optional. But: be sure to have one. That's always safer. You can set it like the key: app.use(session({ secret: 'pinkie ate my cupcakes!' }));

passport [function] optional:

Defaults to require('passport'). If you want, you can provide your own instance of passport for whatever reason.

socket.request.user

This property is always available from inside a io.on('connection') handler. If the user is authorized via passport, you can access all the properties from there. for example:

io.on('connection', function(socket){
    console.log(socket.request.user);
}

Plus you have the socket.request.user.logged_in property which tells you whether the user is currently authorized or not.

Additional methods

passportSocketIo.filterSocketsByUser

This function gives you the ability to filter all connected sockets via a user property. Needs two parameters function(io, function(user)). Example:

passportSocketIo.filterSocketsByUser(io, function(user){
  return user.gender === 'female';
}).forEach(function(socket){
  socket.emit('messsage', 'hello, woman!');
});

Clientside:

You have to provide the session-cookie. If you haven't set a name yet, do it like this: app.use(session({ key: 'your.sid-key' })); For example, my Angular.js implementation

appServices.factory('socket', ['$rootScope',function ($rootScope) {
    var socket = io.connect(); // Connect to server

    return {
        on: function (eventName, callback) {
            socket.on(eventName, function () {
                var args = arguments;
                $rootScope.$apply(function () {
                    callback.apply(socket, args);
                });
            });
        },
        emit: function (eventName, data, callback) {
            socket.emit(eventName, data, function () {
                var args = arguments;
                $rootScope.$apply(function () {
                    if (callback) {
                        callback.apply(socket, args);
                    }
                });
            })
        }
    };
}]);

Serverside:

Nope, there's nothing to do on the server side. Just be sure that the cookies names match.

License

Licensed under the MIT-License. 2012-2015 Filip Lukac.

Library base on passport.socketio