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

lore-hook-websockets

v0.13.0

Published

A lore hook that contains action blueprints for using websockets

Downloads

6

Readme

lore-hook-websockets

WARNING: This hook is the original v1 test implementation of websocket integration for Lore, and IS NOT intended to be used in a project. If you want to use websockets, use one of the lore-hook-websockets-* hooks (for Rails, Sails or Socket.io) or create your own hook using the lore-websockets library.

A Lore hook that generates actions usable with the WebSockets implementation in Sails.

This is the first implementation of this hook, and currently Sails is the reference implementation for the WebSockets interface. In the future it will be expanded to account for other implementations (such as ActionCable in Rails), with the goal of creating an interface that can be adapted to any (convention abiding) WebSocket implementation.

As a worst case scenario, if there ends up being no sensible common abstraction, there will need to be multiple hooks like lore-hook-websockets-sails, lore-hook-websockets-rails, etc.

Usage

The steps below describe how to use this hook.

Register the Hook

First, tell Lore you want the hook to be loaded by adding a reference to it in the index.js file at the root of your project:

Lore.summon({
  hooks: {
    websockets: require('lore-hook-websockets')
  }
});

Install Packages

Next you'll need to install two packages:

npm install socket.io-client --save
npm install sails.io.js --save

Create Initializer File

Next, create an initializer file that will configure the websocket connection when Lore boots up. You can call it whatever you want, but we'll call it initializers/websockets.js for this README. Because sails.io.js attempts to connect to the server as soon as it's created, we need to set the url for the websocket connection immediately after it's created (before it has a chance to connect). We also need to expose the io variable as a global for now, though in the future it will likely be attached to lore like lore.websockets.io.

// initializers/websockets.js
var SocketIOClient = require('socket.io-client');
var SailsIOClient = require('sails.io.js');

module.exports = function() {
  var io = SailsIOClient(SocketIOClient);
  io.sails.url = 'http://localhost:1337';
  window.io = io;
};

Subscribe to Endpoints

Finally, you need to subscribe to the endpoints you want to listen to in your app. For that, create a componentDidMount method in components/Master, and subscribe to your endpoints:

// components/Master.js
  ...
  componentDidMount: function() {
    lore.websockets.posts.subscribe();
  },
  ...

For Sails, the call above (lore.websockets.posts.subscribe()) would make a GET call to http://localhost:1337/posts, which is how you subscribe to data in Sails by default.

Authentication (optional)

If your server uses token based authentication, you will need to configure the io connection to use the appropriate headers. For this example, we'll set the header before we subscribe to any endpoints in our Master component.

// components/Master.
  ...
  componentDidMount: function() {
    io.sails.headers = {
      authorization: 'Bearer ' + localStorage.userToken
    };
    lore.websockets.posts.subscribe();
  },
  ...