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 🙏

© 2026 – Pkg Stats / Ryan Hefner

socket.io-manager

v1.0.0

Published

manage your socket events easily

Readme

socket.io-manager

Manage your socket events easily.

Usage (example)

As first step, let's install socket.io and socket.io-manager:

npm i socket.io socket.io-manager -S

Or:

yarn add socket.io socket.io-manager

Now we need socket server. Create a file called index.js.

// index.js
import socketIO from 'socket.io';

const io = socketIO(9090);

It is time to use socket.io-manager.

Create a file and name it foo.js and import SocketEvent from socket.io-manager and then set up the socket event manager like below.

// foo.js
import { SocketEvent } from 'socket.io-manager';

let socket = new SocketEvent();

// default namespace is '/', if you want change, use SocketEvent.namespace method.

socket
.name('foo')
.middleware(
  (next, { socket }) => text => {
    text === 'you can' ? next() : socket.emit('error', 'I can not do it');
  }
).handler(({ socket }, nsp) => text => {
  nsp.emit('foo', text);
});

now let's back to our index.js and import our socket event manager. we can connect io and our socket events together with connect function provided by socket.io-manager.

// index.js
import socketIO from 'socket.io';
import { connect } from 'socket.io-manager';
import foo from './foo';

const io = socketIO(9090);

// we can connect io to our socket routers with connect function
connect(io, [foo]);

It's done. we can organize our socket like this now. I hope you enjoy it.

API

class SocketEvent

Usage

let socket = new SocketEvent();

Methods

  • namespace(namespace): set namespace. default value of namespace is /, if you don't want to change namespace don't call this method.

    • namespace: type String, required.
  • name(name): set event name for this routers.

    • name: type String, required. event name.
  • middleware(middlewares): add middlewares for this event.

    • middlewares: type Array, required. middlewares.

      How to write a middleware ? it's simple. first look at this example.

      export default (next, { shared, socket, nsp, io }) => (user, msg) => {
        socket.user = user;
        msg = msg + user.name;
      
        next();
      }

      first, it takes 4 arguments, next calls next middleware or handler. socket obviously is socket object. nsp is current namespace scoket.io object. io is main socket.io object. shared is an Object which is created and shared when event is fired till event gets done.

      then it return a function that this function take some arguments. these are event data. for example if in client side I run this code, socket.emit('foo', { name: 'ali' }, 'hey there'), the second function take 2 arguments based on what I've sent to the event. user argument is { name: 'ali' } and msg is hey there.

      that's all.

  • handler(handler): it's main socket handler.

    • handler: type Function, required.

    it looks like middleware function with little difference, it doesn't have next argument.

connect

Usage

connect(io, sockets);

Parameters

  • io: socket.io server class.
  • sockets: type Array, required. array of socket events created by SocketEvent class.

applyMiddleware

if you want to add same middlewares as head middlewares in socket events, it's better to use this function. this function add middlewares before all middlewares in socket events.

Usage

applyMiddleware(middlewares, socketsEvents);

Parameters

  • middlewares: type Array, required. array of middlewares to apply on socket events.
  • socketEvents: type Array, required. array of socket events.

Example

suppose that we have 2 middlewares and one socket event named foo. now we want add these two middlewares in foo socket event. in code language it will be something like this:

applyMiddleware([ middleware1, middleware2 ], [ foo ]);