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

hapi-socketio-router

v1.0.2

Published

A hapi plugin for socket.io that accepts an array of JSON definitions of namespace/events to register with socket.io. Great for modularizing your namespace definitions.

Downloads

8

Readme

hapi-socketio-router

About

A hapi plugin for socket.io that accepts an array of JSON definitions of namespace/events to register with socket.io. Great for modularizing your namespace definitions.

This plugin was inspired by hapi-router by @bsiddiqui. In fact, it borrows from his code. ;) Thank you,bsiddiqui!

Installation

npm install hapi-socketio-router

Usage

First, let's create a directory with registration modules. For instance, if we have socket/chat.js and socket/something-else.js, the chat.js might look like:

socket/chat.js

module.exports = [
    {
        namespace: "/chat",
        events: [
            {
                event: "connection",
                handler: function (socket) {

                    socket.on('hello', function (data) {
                        console.log(data);
                        socket.emit("news", {message:'We got your request on namespace: ' + socket.nsp.name});
                    });


                    socket.on('disconnect', function (data) {
                        console.log('user disconnected from '  + socket.nsp.name);
                    });

                    console.log('user connection to '  + socket.nsp.name);

                }
            }
        ]
    }
];

As you can see, the module is defined as an array of configurations by namespace. This allows you to put all your namespaces into a single file, or you can break them out into individual files.

Now, let's load our events:

var server = new hapi.Server();
server.connection({
    host: 'localhost',
    port: 3000,
    labels: 'primary',
    routes: {
        cors: {
            origin: ['*:*']
        }
    }
});

server.register(
    {
        register: require('hapi-socketio-router'), 
        options: {config: {}, namespaces: 'socket/*.js'}
    }, 
    {select: "primary"}, 
    function (err) {
        if (err) {
            console.log(err);
            throw err;   
        }
    }
);

Specification

Valid options

Valid options which are passed to the plugin include the following. Please see glob for details on glob specific options.

  1. cwd - (deprecated) Instructs the plugin to change to this directory, otherwise load namespace files from project root + namespaces path parameter. This is passed to glob.
  2. ignore - (deprecated) Another glob option.
  3. options - (deprecated) Socket.io options that will be passed to the socket initialization.
  4. config - (optional) Object representing all configuration options.
    1. io - (optional) Socket.IO related configuration.
      1. server - (optional) A Socket.IO object, not yet attached to the Hapi server. By default the plugin creates it's own Socket.IO server.
      2. options - (optional) Options passed to Socket.IO's Server#attach(srv:http#Server, opts:Object):Server method. Please see Socket.IO documentation for Server(opts:Object).
    2. glob - (optional) Object representing glob options accepted by this plugin. Those options are:
      1. cwd - (optional) Sets the working directory to search for namespace files. Defaults to process.cwd().
      2. ignore - (optional) Add a pattern or an array of patterns to exclude matches. Defaults to false. See glob's Comments and Negation documentation.
  5. namespaces - (required) A glob path to namespace files. Example: socket/*.js. Also, see doc/example.js for a namespace file example.
  6. debug - (optional) Turns on debugging mode. Default output goes to console.log()
  7. logger - (optional) A callback function that overrides debug's default console output. Accepts a single string parameter.

Example plugin options

var pluginOptions = {
    debug: true,
    logger: function(message) {console.log(message);},
    config: {
        io: {
            server: undefined, 
            options: {  
                serveClient: undefined, 
                path: undefined
            }
        },
        glob: {
            cwd: undefined, 
            ignore: undefined 
        }
    },
    namespaces: 'server/sockets/*.js'
};
hapiServer.register({register: require('hapi-socketio-router'), options: pluginOptions});

Namespace files

Namespace files are defined as an array of objects.

module.exports = [
    {
        namespace: "/some-namespace",
        events: [
            {
                event: "connection",
                handler: function (socket) {

                    // YOUR SOCKET STUFF HERE

                }
            },
            {
                event: "some-other-event",
                handler: function (socket) {

                    // YOUR SOCKET STUFF HERE

                }
            }
        ]
    },
    {
        namespace: "/some-namespace-2",
        events: [....]
    }
];