1.0.2 • Published 2 years ago

socket-io-event-context v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Socket IO Context

Access your socket event context anywhere

Getting Started

Installation

npm install socket-io-event-context

Enabling the plugin

const { Server } = require("socket.io");
const { socketContext, socketContextPlugin } = require("socket-io-event-context")

const io = new Server({ /* options */ });

io.use(socketContextPlugin) // Register the plugin

Usage

function getUser() {
    //Notice how it does not need access to the socket object
    return socketContext.get("user")
}

io.on("connection", (socket) => {

    socket.on("your-event", (...args) => {
        socketContext.set("user", {id: "helloUser"})
        console.log(getUser())
    })
});

Acknowledgements

Essentially a simplified port of fastify-request-context, kudos to them