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

@onehilltech/blueprint-socket.io

v4.75.0

Published

Socket.io module for Blueprint

Downloads

70

Readme

blueprint-socket.io

A Blueprint.js module for Socket.IO

npm version Dependencies

Features

  • Create Socket.IO servers on all (or selective) server connections
  • Bind connections and namespaces to computed properties for easier access
  • Uses Blueprint listeners to handle connection events

Installation

yarn add @onehilltech/blueprint-socket.io

or

npm install @onehilltech/blueprint-socket.io --save

Listening for Connections

The server listens for Socket.IO connections by extending the ConnectionListener class provided by this module, and registering the listener for the socket.io.connection event. The ConnectionListener has two methods you can override:

  • connection(name, socket) - This method is invoked when a new client connects to the server.
  • disconnect(name, socket) - This method is invoked when a client disconnects from the server.

For both methods, the name argument is the name of the connection (from app/configs/server.js); and the socket argument is the client that connected to the server.

// app/listeners/socket.io.connection/logger.js

const { ConnectionListener } = require ('@onehilltech/blueprint-socket.io');

module.exports = ConnectionListener.extend ({
  connection (name, socket) {
    console.log (`socket.io connection on ${name}: ${socket.id}`);
  },
  
  disconnect (name, socket) {
    console.log (`socket.io disconnect on ${name}: ${socket.id}`);
  }
});

Filtering Connections

The connections property on the ConnectionListener is used to filter which connections the listener will react.

// app/listeners/socket.io.connection/logger.js

const { ConnectionListener } = require ('@onehilltech/blueprint-socket.io');

module.exports = ConnectionListener.extend ({
  connections: ['insecure'],
  
  connection (name, socket) {
    console.log (`socket.io connection on ${name}: ${socket.id}`);
  },
  
  disconnect (name, socket) {
    console.log (`socket.io disconnect on ${name}: ${socket.id}`);
  }
});

Emitting Events

The easiest way to emit an event is to bind the connection/namespace to a property on a Blueprint component (e.g., controller, server, etc.) using the io factory method. You can then use the computed property to emit an event.

You can also use the computed property to emit to a room.

const { Action, Controller } = require ('@onehilltech/blueprint');
const { io } = require ('@onehilltech/blueprint-socket.io');

module.exports = Controller.extend ({
  /// Bound to the insecure Socket.IO connection.
  insecure: io (),

  /**
   * Force emit a message to all participants.
   */
  emit () {
    return Action.extend ({
      execute (req, res) {
        const { message } = req.query;

        // Emit an event to all clients on the connection.
        this.controller.insecure.emit ('chat message', message);

        res.status (200).json (true);
      }
    })
  }
});

As shown in the example above, the insecure attribute is bound to the default namespace on the insecure connection. We can the emit the event on the socket.

Emitting to a namespace

To emit to a namespace, you bind the property to a namespace using io ().of (nsp). For example, io ().of ('/chat') will bind the property to the /chat namespace.

Happy Coding!