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.io-namespace-controller

v2.2.2

Published

Wrapper around Socket.IO namespaces which turn them into controller-like objects with advanced abilities

Downloads

26

Readme

socket.io-namespace-controller Coverage Status Build Status

Wrapper around Socket.IO namespaces which turn them into controller-like objects with advanced abilities

Installation

npm i socket.io-namespace-controller

Intro

Main purpose of this library is to compose socket event handlers into objects. So event handler declaration and registration are separated and logic is now structured and encapsulated. Now you can group them into mixins and reuse code across controllers. Also you can easily wrap your methods at one centralized place.

Getting started

First, you need to get setupController function from libray, giving it socket.io Server instance:

const io = require('socket.io')(server);
const setupController = require('socket.io-namespace-controller').driver(io);

setupController(namespace, definition) takes two arguments:

  • namespace - name of socket.io namespace, your controller shall be assigned to
  • definition - object, describing your controller logic. It can consists from following properties:
    • methods - object of methods, which shall be assigned to same named events
    • emitters - object of methods, which shall store specific emitter logic and can be called by other controllers
    • created - hooks, which should be called after namespace created
    • connected - hook, which should be called after each socket connection to namespace

Methods

Let's create very simple controller, which shall handle two events:

  • load - for settings loading by clients
  • update - for settings updating with broadcasting of changed settings to all connected clients.
setupController('/settings', {
  methods: {
    async load() {
      this.emit('data', await settingsService.all());
    },
    async update(data) {
      await settingsService.set(data);
      this.broadcast.emit('data', data);
    }
  }
})

As you can see, Socket instance has been bound as this to our methods.

Emitters

Let's do something more complex and useful for our beloved clients. Client, which changes settings, shall be pleased to see nice green notification on update success.

Such logic must be separated into own controller, do you agree? So let us create controller, which shall have two methods - for emit notifications to sender, and to all except sender:

setupController('/notifications', {
  emitters: {
    notify(message) {
      this.emit('message', message);
    },
    notifyOthers(message) {
      this.broadcast.emit('message', message);
    }
  }
});

and after that we can easily add usage of it into our settings controller by editing update() method:

    async update(data) {
      await settingsService.set(data);
      this.as('/notifications').notify('You have successfully updated settings!');
      this.as('/notifications').notifyOthers('Settings updated.');
      this.broadcast.emit('data', data);
    }

All events of your namespace, ability of emitting which you want to share across other controllers, must be described as part of emitters object. Only emitters object is returned by calling this.as() from your controller method. You shall not have direct access to emit or broadcast of target controller, only through emitters.

It done such way for having all namespace events described only in controller they belong to.

Emitters can be used at their controller methods. For example let's add such method into described above notifications controller:

  methods: {
    async getUnread(clientId) {
      const messages = await notificationsService.getUnreadFor(clientId);
      for (let message of messages) this.emitters.notify(message);
    } 
  }

this Context

Each method and emitter is bound to connected socket instance with following additional properties assigned:

  • as(namespaceControllerName) - method which returns emitters of another controller
  • methods - your controller methods
  • emitters - your controller emitters

Hooks

created(namespace) - is called after namespace creation. receives original socket.io Namespace instance.

That's place for assigning middleware to namespace, etc.

Example:

setupController('/test', {
  methods: { ... },
  created(namespace) {
    namespace.use((socket, next) => {
      if (socket.request.headers.cookie) return next();
      next(new Error('Authentication error'));
    })
  }
})

connected(socket) - is called after client connected, receives original socket.io Socket instance.

That's place for room joining, etc.