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

sockerate

v0.1.2

Published

a minimal package for decorating socket.io listeners

Readme

Sockerate

This minimal package helps you to create socket.io listeners using decorators in Typescript.

Features

  • very minimal and lightweight
  • catching both synchronous and asynchronous errors which are not handled in socket.io
  • makes your code cleaner

Installation

  1. install socket.io and sockerate:
npm install sockerate --save-exact
npm install socket.io
  1. in tsconfig.json set these options:
{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

Usage

  1. create a class and use @On decorator on your method. you can also use @Listener decorator to add a prefix for your class methods. let's take a look at an example:
// in messageListener.ts file
import { On, Listener } from 'sockerate';

@Listener('/message')
export default class MyClass {
  @On('/new')
  newMsg(socket: any, data: any, callback: any) {
    // your logic
  }
}

// or you can ignore Listener decorator
export default class MyClass {
  @On('/message/new')
  newMsg(socket: any, data: any, callback: any) {
    // your logic
  }
}

each method takes 3 arguments:

  • socket
  • data: which you take from client. client also need to put all the data in this argument.
  • callback: which you can use as the last argument to be the Acknowledgement
  1. now you need to pass your listeners as an array to setListeners as example:
// in socket.ts file
import { Server } from 'socket.io';
import { setListeners } from 'sockerate';
import MyClass from './messageListener';

const io = new Server(3000);

io.on('connection', (socket) => {
  console.log('new connection');
  setListeners(socket, { listeners: [MyClass] });
});

now you should be able to exchange data with cleints in /message/new eventName:

const { io } = require('socket.io-client');

const socket = io('ws://localhost:3000');

socket.emit('/msg', 'msg', (res) => {
  console.log(res);
});

Methods

for now only @On and @Once decorators (whcich are equivalent of socket.on() and socket.once() methods) are handled. but you can use other socket.io methods as before alongside this package.

ErrorHandler

by default both synchronous and asynchronous errors will be catched. but it is recommended to define your errorrHandler and pass it to setListeners as below:

// define your errorHandler
function errorHandler(err: any, callback: any) {
  console.log(err);
  /* MAKE SURE to check callback type to be a function
     cuz it also depends on client to use it
     and your app will have UNCATCHED ERROR  if client don't use it */
  if (typeof callback === 'function') callback({ err });
}

//pass it to setListeners
setListeners(socket, { listeners: [MyClass], errorHandler });

GetLisener

to use some of the methods such as socket.off() (see here) you need your listeners.

for this you can use getLisener(Class, propertyKey) to get the listener. for example to get the listener defiend in first example:

import MyClass from './messageListener';
import { getLisener } from 'sockerate';

const listener = getLisener(MyClass, 'newMsg');

// now you can use it in socket.off()
socket.off('/message/new', listener);

Options

two options are provided:

  • catchError: default value is true. you can disable this option and handle error in each method by your own
  • prefix: you can set it to add a prefix behind all eventNames

you can change them in setListeners:

const options = {
  catchError: true,
  prefix: 'something',
};
setListeners(socket, { listeners, err }, options);