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

socket-helper

v1.0.0

Published

Socket.io helper utilities with events inspired by flux standard actions.

Downloads

6

Readme

Socket Helper

Socket.io helper utilities with events inspired by flux standard actions. This package is targeted towards socket server applications and not client applications.

Why Socket Helper?

SocketIO provides a large set of tools to run websocket servers with ease. What Socket Helper provides is a wrapper for SocketIO with connection, new connection, disconnection and basic message events preconfigured.

Installation

The package is available in the npm registry. To install, use npm or yarn.

$ npm install --save socket-helper

$ yarn add socket-helper

Usage

Once installed, make sure that socket.io is also installed as this package does not come with it. This will not work without socket.io.

Basic Usage

To use Socket Helper, pass io from socket.io into it.

var io = require('socket.io')(http);

var socketHelper = require('socket-helper');

socketHelper(io);

Custom Listeners

Socket Helper comes with connection and disconnection event listeners by default. We can add our own listeners in addition to the existing ones.

function onConnection(socket, payload) {
  console.log('Connected', payload);
}

function onDisconnection(socket, payload) {
  console.log('Disconnected', payload);
}

socketHelper(io, onConnection, onDisconnection);

Configs and Overrides

We can customize the default configs used by Socket Helper by providing a custom config.

var config = {
  clients: [],
  overrides: {
    message: true,
    connection: true,
    disconnection: true,
    newConnection: true
  }
};

socketHelper(io, onConnection, onDisconnection, config);

clients

The clients key stores the ids of currently active connections.

overrides

Overrides define whether in-built event handlers for message, connection, disconnection and newConnection are disabled or not.

API

When a new connection is established, the socket server emits two events, one for the new connection and another for all other connected clients.

The data send with the socket event will be FSA compliant, i.e., will have the following format:

{
  type: 'TYPE',
  payload: {
  }
}

The socket event will always emit events of name message. The data with the event will carry the information about how the event should be handled, similar to Flux actions.

CONNECTION

| Event | Type | Payload | | --------- | ---------------- | ----------------------- | | message | CONNECTION | { clientID, clients } | | message | NEW_CONNECTION | { clientID, clients } |

DISCONNECTION

| Event | Type | Payload | | --------- | --------------- | ----------------------- | | message | DISCONNECTION | { clientID, clients } |

MESSAGE

The server is configured by default to listen for and respond to events with name message. So when a client emits a message event, the server responds to it by broadcasting the message to all the clients including the sender.

| Event | Type | Payload | | --------- | --------- | ----------------------------------------------------------- | | message | MESSAGE | {clientID, clients, event: data.type, data: data.payload} |

The server expects client to also send the message event with data in FSA format. The data sent by client, will be mapped as event: type and data: payload when it is sent to all connected clients.

Contributing

To contribute, follow one of the two options:

  • Open an Issue

    Open an issue detailing:

    1. What the issue is
    2. Steps to reproduce
    3. Possible solutions

    Note: These details are recommended but are entirely optional.

  • Send a Pull Request

    Fork this project and send a pull request to the master branch.

LICENSE

MIT