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

socket.io-mysql-session

v1.0.9

Published

Session manager for socket.io and websockets using MySQL as a session store. This was designed specifically for applications that use frameworks like PhoneGap, where cookies are unreliable after the app is compiled.

Downloads

20

Readme

Socket-io-mysql-session is a session manager for socket.io and websockets using MySQL as a session store. This was designed specifically for applications that use frameworks like PhoneGap, where cookies are unreliable after the app is compiled.

This is designed as middleware for socket.io. When a socket connects, it creates a new instance of itself and attaches to socket.session. From there, you can interact with the session on the server side.

Default Events

By default, the middleware creates a "getToken" listener. This listener waits for the client to send a "getToken" event along with an object that contains a token. It then tries to load the session. However, if the session cannot be found or is expired, it will create a new session. After all of this is done, it will then emit a "gotToken" with an object that contains a token (either a new one or the one you passed in), and any error messages encountered:

{token: "foo", errors: ""}

Usage - Server Side

To instantiate the session manager and add it to socket io:

Example

var socketSession = require("socket.io-mysql-session"),
    app = express(),
    http = require('http').Server(app),
    Logger = require("filelogger"),                   //filelogger is not required, but supported by the middleware
    logger = new Logger("error", "info", "my.log"),
    io = require("socket.io")(http),
    mysql = require("mysql"),
    options = {
        host: "localhost",
        port: 3306,
        user: "root",
        password: "root",
        database: "foo"
        },
     db = mysql.createConnection(options),

//add the middleware

io.use(new socketSession({
    db: db,           //MySQL conneciton - required
    logger: logger    //filelogger - optional
    expiration: 3600  //expiration time in seconds - optional - defaults to 86400000
}));

Usage - Interaction with the session

To get and set items to the session scope, there are two handy functions included -- get and set. Set will automatically save the session to the database when called.

API

Function | Parameters | Description -------- | ---------- | ----------- set | key, value | Saves into the session at Key, Value -- persists to database, updates expires get | key | Retrieves Key from the session. If it doesn't exist, returns "" clearSession | | Clears session data from the socket.

Example

io.on("connection", function(socket){
    socket.on("user:login", function(params){
        //do login stuff here with params
        var userId = "id from code above";
        socket.session.set("userId", userId);
    });

    socket.on("user:securedEvent", function(params){
        var userId = socket.session.get("userId");

        if(userId != ""){
            //do secured user stuff
        } else {
            //throw error
        }
    });
});

Usage: Client Side

Since we're not using cookies, we have to do a little work on the client to initialize the session -- including storing the session token. Since my goal was to use this with a compiled app, local storage seemed like a good choice for the store. All we need to do is emit a "getToken" event and listen for a "gotToken" event.

Example


var socket = io("http://your.server.here"),
    token = localStorage.getItem("token") || "";

socket.emit("getToken", {token: localStorage.getItem("token") || ""});

socket.on("gotToken", function(message){
    console.log(message);
    if(message.token != "") {
        localStorage.setItem("token", message.token);
        token = message.token;
    }
});

That's it. You're now using sessions with websockets :).

Note

This middleware does NOT create its own database table. I'm of the mindeset that apps shouldn't ever have that level of database access, so you need to create it on your own. You'll get the create statement back in error messages if it encounters an error, but here it is:


CREATE TABLE `session` (
  `sessionId` varchar(32) COLLATE utf8_bin NOT NULL,
  `expires` int(11) unsigned NOT NULL,
  `data` text COLLATE utf8_bin,
  PRIMARY KEY (`sessionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;