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

express-session-synchronize-socket.io

v0.3.11

Published

express session synchronize with socket.io , auto render HTTP session with micro-service

Downloads

25

Readme

express-session-synchronize-socket.io

npm npm downloads npm version

Build Status codecov

express middleware session can be attach to socket.io have an synchronizer session and can dist session HTTP with micro-service

Installation

npm i express-session-synchronize-socket.io --save

yarn add express-session-synchronize-socket.io

usage:

server.js

const
    exp = require('express')
    ,app = exp()
    ,server = require('http').Server( app )
    ,session = require('express-session-synchronize-socket.io')()
    , io = require('socket.io')( server )
;

app
    // ... you'r other middlewares
    .use( session.express( /* default value session optional */ ) )
    // ... you'r other middlewares
;

app.get('/' , (req,res) => {

    const val = req.session.val ; // session HTTP access

    if( !val.id ) {

        req.session.val.id = Math.random() ; // define one key id for current session

        // manual synchronize session with tcp store
        req.session.synchronize( {
            model: "http"
            ,target: "tcp"
        } ) ;
    }

    console.log('from HTTP: ' , val.id ) ;

    res.sendFile('./index.html') ;

} ) ;

io
    // ... you'r other middlewares
    .use( session.socketIO( /* default value session optional */ ) )
    // ... you'r other middlewares
;

io.on('connect' , socket => {

    const val = socket.session.val;

    // 0.xxx... , after request on "/" ,  because HTTP controller have synchronize memory strore
    console.log( 'from TCP: ' , val.id ) ;

} ) ;

server.listen( 80 , () => console.log('server run ...') ) ;

index.html

<html>

    <head>
        <meta charset="utf-8">
        <title>index</title>
    </head>

    <body>

        <main>
            <h1>Lorem Ipsum</h1>
        </main>

        <!-- only if you use socket.io module -->
        <script src="/socket.io/socket.io.js"></script>
        <script>
            const socket = io('/') ;
        </script>
    </body>
</html>

middlewares optional config arguments


    // ... ,
    session = require('express-session-synchronize-socket.io')(
        /* [pathGetSession:string] default */ "/get/session"
        /* return HTTP session in micro-service give null or false , if you want an manual control  */

        /* [autoSynchro:bool] default */ true
        /* synchronize HTTP session with TCP session between HTTP request you can manual use synchonize session */
    )
    // ... ,

custom you access to session values

server.js

const
    exp = require('express')
    ,app = exp()
    ,server = require('http').Server( app )
    ,session = require('express-session-synchronize-socket.io')()
    , io = require('socket.io')( server )
;

app
    // ... you'r other middlewares
    .use( session.express(
        {} /* default value session optional */
        "data" , // name key session access
    ) )
    // ... you'r other middlewares
;

app.get('/' , (req,res) => {

    const data = req.session.data ; // access with data key now

    if( !data.id ) {

        req.session.data.id = Math.random() ; // define one key id for current session

        // manual synchronize session with tcp store
        req.session.synchronize( {
            model: "http"
            ,target: "tcp"
        } ) ;
    }

    console.log('from HTTP: ' , data.id ) ;

    res.sendFile('./index.html') ;

} ) ;

io
    // ... you'r other middlewares
    .use( session.socketIO(
        {} /* default value session optional */
        , "data" // name key session access
    ) )
    // ... you'r other middlewares
;

io.on('connect' , socket => {

    const data = socket.session.data; // access with data now

    // 0.xxx... , after request on "/" ,  because HTTP controller have synchronize memory strore
    console.log( 'from TCP: ' , data.id ) ;

} ) ;

server.listen( 80 , () => console.log('server run ...') ) ;

GET /get/session JSON

access to HTTP session from default route use you HTTP session with an micro-service and easier build view render with Reactjs , Angularjs , Emberjs or other framework/library Javascript

custom micro-service HTTP session

config middleware


    // ... ,
    session = require('express-session-synchronize-socket.io')(
        null, // give null for not default route access session
        true
    )
    // ... ,

define your routes with express e.g :

    // ... ,
    app
        .get('/get/session/avatar' , ( req , res ) => {
            // return only avatar session

            res.json( req.session.user.avatar || null ) ;
        } )
        .get('/get/session/username' , ( req , res ) => {

            res.json( req.session.user.username || null )
        } )
        .get( '/get/session/create-at' , ( req , res ) => {

            res.json( req.session.user.createAt ) ;
        } )
        .get('/get/session/all' , ( req , res ) => {

            // all is only user here

            res.json( req.session.user ) ;
        } )
    ;

session HTTP is not save in cookies this package use local state of Nodejs for an memory store

you can thus easily imagine a structure using a notion of session server and client session the client session data being distributed in micro service for example where sent to your template in a more traditional way

develop by Samuel Gaborieau with <3 and Nodejs for open source and enjoy