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

rf-api-websocket

v0.2.3

Published

rf-api implementation for RPC over websockets using JSON messages with optional ACL

Downloads

8

Readme

rf-api-websocket

rf-api implementation for RPC over websockets using JSON messages with optional ACL.

ws server

  • Uses express server instance
  • Websocket communication service
  • Allows safe RPC
  • Uses JSON messages

ws methods

addHandler

Add a simple callback handler. Errors can be signalled via exceptions Any previous handler with the same func name will be replaced.

services.addHandler (funcName, callback, acl = {})
  • funcName The name of the handler. In order for the handler to be called, this needs to be used in the func attribute of the received websocket message.
  • callback The handler function(msg, responseCallback(msg))
  • acl Optional ACD configuraton

addPromiseHandler

Add a promise callback handler that either resolves to null (no response) or to response data and signals exceptions via rejection (logged, no response. Any previous handler with the same func name will be replaced.

services.addPromiseHandler (funcName, genPromise, acl = {})
  • funcName The name of the handler. In order for the handler to be called, this needs to be used in the func attribute of the received websocket message.
  • callback The handler function(msg, responseCallback(msg))
  • acl Optional ACD configuraton

sendObj

services.sendObj (ws, obj)

TODO: integrate callbackID maybe one raw send method, and one preconfigured (default to use)

broadcast

Send the given object to ALL the currently connected websockets NOTE: This sends the object as-is.

services.broadcast (obj)

Getting started

start the package

When the module is started, the websocket server and handler is automatically registered against the HTTP server. You dont need to start the server manually!



// prepare backend
var config = require('rf-config').init(__dirname); // config
var http = require('rf-http').start({ // webserver
   pathsWebserver: config.paths.webserver,
   port: config.port
});
var API = require('rf-api').start({app: http.app}); // prepare api
var mongooseMulti = require('mongoose-multi'); // databases
var db = mongooseMulti.start(config.db.urls, config.paths.schemas);


db.global.mongooseConnection.once('open', function () {

   // optional: start access control; has to be done before starting the websocket
   require('rf-acl').start({
      API: API,
      db: db,
      app: http.app,
      sessionSecret: dbSettings.sessionSecret.value
   });

   // start websocket connection;
   require('rf-api-websocket').start({API: API, http: http});

   // start requests
   API.startApiFiles(config.paths.apis, function (startApi) {
      startApi(db, API, services);
   });
});

Use Websocket requests

Websocket messages have the form: {func: "", data: {...}, token: ""} Any other attributes are copied to the response

Register a handler like this:

API.onWSMessage("myfunc", (msg, respondWS, userInfo) => {
    // userInfo contains the object extracted from the JWT (or {} if no token was supplied)
    // If the user does not have the required permissions or the message is malformed,
    // this function is not called but instead an error msg is sent!
    if(!userInfo.isAdmin) {
      return false;
    }
    // Handle message (msg is .data of the original message)
    console.log(msg.foobar)
    // Then send response. Convention is to have an err attribute
    respondWS({err: null, info: "It works"});
    // NOTE: You can call respondWS multiple times if required!
}, {}) // Empty ACL => no auth required

See the rf-acl package for documentation about the ACL syntax

If you are using Promises, use this syntax

API.onWSMessagePromise("myfunc", (msg, respondWS, userInfo) => {
  return new Promise((resolve, reject) => {
    if(!userInfo.isAdmin) {
      return reject("nope"); // Will send {err: "nope"}
    }
    return resolve({"foo": "bar"}); // will send {err: null, "foo": "bar"}
  });
}, {}) // Empty ACL => no auth required

Development

Install the dev tools with

npm install

Then you can runs some test cases and eslint with:

npm test

Generate Docs:

npm run-script doc

To Do

  • get the everything running

Legal Issues

  • License: MIT
  • Author: Rapidfacture GmbH