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

c2b-session

v1.0.8

Published

A legacy yet minimalist session-manager for Node.js/IO.js. Supports Node.js/IO.js from version 0.10 onwards.

Downloads

9

Readme

c2b-session

A legacy yet minimalist session-manager for Node.js. Supports Node.js/IO.js from version 0.10 onwards.

Unit-Tests are included.

Build Status npm version


Table of contents


API

Methods (fn) of the module:

* setTimeout(time <minutes>, callback <function>(ident, time)) Fn
* create(config <object>, callback <function>(err, session)) Fn
* retrieve(ident <string>, callback <function>(err, session)) Fn
* createOrRetrieve(config <object>, callback <function>(err, session, state)) Fn
* exists(ident <string>) Boolean
* is_connected(ident <string>) Boolean
* getAll() Object
* getOnline() Array
* destroy() Void

Methods (fn) of a session-object:

* connect(callback <function>(err)) Fn
* disconnect(callback <function>(err)) Fn
* online() Boolean
* put(callback <function>(err)) Fn
* get([key <string> (optional)], callback <function>(err, result)) Fn

Default template of session-object:

{
    t_connect: Date.now(),      //timestamp of a sessions initial connection
    t_last_action: Date.now(),  //timestamp of a sessions last action, eg. put() [required]
    connected: false,           //state of a sessions connection [required]
    ident: '',                  //ident of a session [required]
    data: {}                    //storage of session data [required]
}

Setup

var sessionManager = require('@burnett01/c2b-session');

Set a timeout for sessions (optional)

//Optionally set a timeout (min) for sessions...
sessionManager.setTimeout(1);

//as of version 0.0.3 you can pass a function too
sessionManager.setTimeout(1, function(ident, time){
    console.log(`[SESSION] ID ${ident} timed-out (${time})`);
});

Create a session

sessionManager.create({
    ident: "my-session-name"

}, function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully created!");
    console.log(session);
});

//You may also pass some default data to your session

sessionManager.create({
    ident: "my-session-name",
    data: {
        A: "Data 1",
        B: "Data 2",
        C: "Data 3"
    }

}, function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully created!");
    console.log(session);
});

Retrieve a session

sessionManager.retrieve("my-session-name", function(err, session){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully retrieved!");
    console.log(session);
});

Create or retrieve a session

sessionHandler.createOrRetrieve({
    ident: "my-session-name"

}, function(err, session, state){
    if(err){ return console.log(err); };

    if(state == 1){ console.log("Successfully created!"); }
    if(state == 2){ console.log("Successfully retrieved!"); }
    
    console.log(session);
});

Test whether session exist

if(sessionManager.exists("my-session-name")){
    console.log("[SESSION] Exist!");
}else{
    console.log("[SESSION] Invalid session!");
}

Test whether session is connected

if(sessionManager.is_connected("my-session-name")){
    console.log("[SESSION] Online!");
}else{
    console.log("[SESSION] Offline!");
}

Get all sessions

sessionManager.getAll();

//or
sessionManager.sessions;

Get online sessions

sessionManager.getOnline();

Destroy a session

sessionManager.destroy("my-session-name");

Session Handling

A session-object is returned by the modules create and retrieve methods.

Connect a session

session.connect(function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully connected!");
});

Disconnect a session

session.disconnect(function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully disconnected!");
});

Test status

if(session.online()){
    console.log("[SESSION] Online!");
}else{
    console.log("[SESSION] Offline!");
}

Store session-data

session.put({ 
    D: "Data 4",
    E: "Data 5",
    F: "Data 6"

}, function(err){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully stored the data!");
});

Query session-data

//Query by key
session.get("E", function(err, result){
    if(err){ return console.log(err); };
    
    console.log("[SESSION] Successfully fetched the data!");
    console.log(result);
});

//Query all
session.get(function(err, result){
    if(err){ return console.log(err); };

    console.log("[SESSION] Successfully fetched the data!");
    console.log(result);
});

Example 1

Create a session if it does not exist, otherwise retrieve the exisiting session:

if(!sessionHandler.exists("my-session-name")){

    sessionHandler.create({
        ident: "my-session-name"

    }, function(err, session){
        if(err){ return console.log(err); };
        console.log("[SESSION] Successfully created!");
        console.log(session);

        //You may now connect via session.connect(...)
    });

}else{

    sessionHandler.retrieve("my-session-name", function(err, session){
        if(err){ return console.log(err); };
        console.log("[SESSION] Successfully retrieved!");
        console.log(session);

        //You may now connect via session.connect(...)
    });
}

Example 2 (recommended)

This example is the recommended approach.

sessionHandler.createOrRetrieve({
    ident: "my-session-name"

}, function(err, session, state){
    if(err){ return console.log(err); };

    if(state == 1){ console.log("Successfully created!"); }
    if(state == 2){ console.log("Successfully retrieved!"); }

    if(!session.online()){
        session.connect(function(err){
            if(err){ return console.log(err); };
            console.log("Successfully connected!");
        });
    }

});

How to install:

Use npm install @burnett01/c2b-session


Unit-Tests

The testing-framework used by this module is Mocha with the BDD / TDD assertion library Chai.

  • test/test.default.js Performs 13 tests | Source

Simply run those tests as you please:

Make

make test

NPM

npm test


Use-case:

This module is used for a Twitch-like channel application.


Contributing

You're very welcome and free to contribute. Thank you.


License

MIT