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 🙏

© 2025 – Pkg Stats / Ryan Hefner

socknet

v5.0.0

Published

PropTypes style for secure your socket.io application

Downloads

126

Readme

Build Status Dependency Status NPM version npm

NPM

Overview

Socknet hook any socket.io like library that allow you to use your favourite validation lib before calling your all your events.

It's inspired by react component declaration style for event creation focusing on lisibility, modularity and security. Fully compatible with socket.io client it works on every platform, browser or device, focusing equally on reliability, and speed.

How to use

Installing

$ npm install --save socknet

Parametes

Socknet constructor take an options object before all socket.io Server constructor parameters:

{ schemas: schemas indexed by eventName, validate: a validation function which receive schema and data to validate. Must throw on error, could be async }

Examples

Basic usage

server.js

import { Socknet } from 'socknet';
import * as Joi from 'joi';


// Note that schema is an array of all args
const schema = Joi.array().ordened([
  Joi.object({
    data: Joi.string(),
  }),
  Joi.object({
    data2: Joi.string(),
  }),
])

const socknet = new Socknet({ schemas: { '/test': schema }, validate: (schema: Joi.AnySchema, data: unknown[]) => schema.validateAsync(data) }, httpServer)
const socknet.listen(PORT)

const testEvent = (...args: any[]) => {
  console.log('received and validated args', args);
}

socknet.on('connection', (socket) => {
  // event /test now have arguments protection
  socket.on('/test', testEvent);
});

client.js

import { Socket } from 'socket.io-client';

Socket = io('http://localhost:' + PORT) as unknown as Socket;

Socket.emit('/test', { data: 'test' }, { data2: test2});

Server should log

[{ "data": "test" }, { "data2": "test2" }]

Note that in this case invalid data will cause request is cancelled see below for error handling

Response callback

server.js

import { Socknet } from 'socknet';
import * as Joi from 'joi';


// Note that schema must contain callback function to allow it
const schema = Joi.array().ordened([
  Joi.string(),
  Joi.func()
])

const socknet = new Socknet({ schemas: { '/test': schema }, validate: (schema: Joi.AnySchema, data: unknown[]) => schema.validateAsync(data) }, httpServer)
const socknet.listen(PORT)

const testEvent = ((username, eventCallback) => {
    const error = doSomething();

    // Here whe handle both cases with callback or not but you could validate the function as `Joi.func().required()` and always have it
    if (eventCallback) {
        if (error) eventCallback(error)
        else eventCallback(null, username)
    }
})

socknet.on('connection', (socket) => {
  // event /test now have arguments protection
  socket.on('/test', testEvent);
});

client.js

import { Socket } from 'socket.io-client';

Socket = io('http://localhost:' + PORT) as unknown as Socket;

Socket.emit('/test', 'someusername', function (error, response) {
  if (error) {
    // handle error
  } else {
    console.log(response);
  }
});

Client should log

["someusername"]

Ajv


import { Socknet } from 'socknet';
import Ajv, { JSONSchemaType } from 'ajv';

import { Location } from '@/types';

const LocationSchema = {
  type: 'array',
  items: [
    {
      type: 'integer',
    },
    {
      type: 'integer',
    },
  ],
  minItems: 2,
  maxItems: 2,
} as JSONSchemaType<Location>;


export const ajv = new Ajv({
  schemas: {
    location: LocationSchema,
  },
});

const socknet = new Socknet(
  {
    schemas: {
      '/test': 'location',
    },
    validate: (schema, data) => {
      try {
        console.log(ajv.validate(schema, data));
      } catch (error) {
        console.log(error);
        throw error;
      }
    },
  },
);

const socknet.listen(PORT)

const testEvent = ((longitude, latitude, eventCallback) => {
    const error = doSomething(longitude, latitude);

    if (eventCallback) {
        if (error) eventCallback(error)
        else eventCallback(null, username)
    }
})

socknet.on('connection', (socket) => {
  // event /test now have arguments protection
  socket.on('/test', testEvent);
});

client.js

import { Socket } from 'socket.io-client';

Socket = io('http://localhost:' + PORT) as unknown as Socket;

Socket.emit('/test', 1.184646, 50.123415);