New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lore-utils

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lore-utils

Functions and files used across multiple Lore packages

  • 0.10.1
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by66.67%
Maintainers
1
Weekly downloads
 
Created
Source

lore-hook-websockets

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();
  },
  ...

Keywords

FAQs

Package last updated on 13 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc