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

sc-broker

v8.0.0

Published

sc-broker key-value and message broker

Downloads

12,612

Readme

sc-broker

scBroker is a lightweight key-value store and message broker. It is written entirely in node.js for maximum portability.

Installation

npm install sc-broker

Overview

To use it call:

var scBroker = require('sc-broker');

Firstly, launch a new scBroker server. If you're using the node cluster module, you might want to launch the scBroker server once from the master process and then interact with it using scBroker clients.

Server

To launch the server, use:

var dataServer = scBroker.createServer({port: 9000, secretKey: 'mySecretKey'})

The secretKey argument is optional; you should use it if you want to restrict access to the server. If you're running a node cluster, you may want to use a random key and distribute it to all the workers so that only your application can interact with the scBroker server.

Once the server is setup, you should create clients to interact with it.

Make sure that the server is running before creating clients

This can be done in the following way:

var conf = {port: 9000}
  , server = scBroker.createServer(conf);

server.on('ready', function () {

 console.log('Server ready, create client');
 var client = scBroker.createClient(conf);

 // do client stuff

});

After all the server provides a destroy function:

server.destroy()

Client

To create a client use:

var dataClient = scBroker.createClient({port: 9000, secretKey: 'mySecretKey'});

The port and secretKey must match those supplied to the createServer function.

Client methods

The client exposes the following methods: (Please see the section on keys to see how you can use keys in scBroker. Also, note that the callback argument in all of the following cases is optional.)

exec

exec(code,[data,] callback)

Run a special JavaScript function declaration (code) as a query on the scBroker server. This function declaration accepts the DataMap as a parameter. This is the most important function in scBroker, all the other functions are basically utility functions to make things quicker. Using exec() offers the most flexibility. The callback is in form:

callback(err, data)

Example:

var queryFn = function (DataMap) {
    // The myMessage variable comes from queryFn.data
    DataMap.set(['main', 'message'], myMessage);
    return DataMap.get(['main']);
};

queryFn.data = {
    myMessage: 'This is an important message'
};

client.exec(queryFn, function (err, data) {
    console.log(data); // outputs {message: "This is an important message"}
});

Note

The query functions are not regular functions. Query functions are executed remotely (on the scBroker server), therefore, you cannot access variables from the outer parent scope while inside them.

To pass data from the current process to use inside your query functions, you need to set them through the data property (see queryFn.data) in example above. Properties of queryFn.data will be available as regular variables inside the query function when it gets executed on the server. All query data is escaped automatically, so it's safe to supply user input. The queryFn.data property is optional.

set

set(keyChain, value, callback)

Set a key-value pair, when the operation has been completed, callback will be executed. The callback is in form:

callback(err)

add

add(keyChain, value, callback)

Append a value at the given keyChain; the object at keyChain will be treated as an array. If a value already exists at that keyChain and is not an array, this existing value will be placed inside an empty array and the specified value argument will be appended to that array. The callback is in form:

callback(err, insertionIndex)

concat

concat(keyChain, value, callback)

Concatenate the array or object at keyChain with the specified array or object (value). The callback is in form:

callback(err)

remove

remove(keyChain,[getValue,] callback)

Remove the value at keyChain. If value is an array, it will remove the entire array. The optional getValue is a boolean which indicates whether or not to return the removed value in the callback. The callback is in form:

callback(err, value)

removeRange

removeRange(keyChain, fromIndex,[ toIndex, getValue,] callback)

Remove a range of values at keyChain between fromIndex and toIndex. This function assumes that the value at keyChain is an object or array. The optional getValue argument specifies whether or not to return the removed section as an argument to the callback. The callback is in form:

callback(err, value)

removeAll

removeAll(callback)

Clear scBroker completely. The callback is in form:

callback(err)

splice

splice(keyChain,[ options,] callback)

This operation is designed to work on Arrays (the keyChain argument should point to an Array). It is similar to JavaScript's Array.splice() function. It can be used to remove and insert elements within an Array. The options argument is an object which can have the following properties:

  • index // The index at which to start inserting/deleting
  • count // The number of items to delete starting from index
  • items // An Array of items to insert at index

Callback form:

callback(err, value)

pop

pop(keyChain,[getValue,] callback)

Remove the last numerically-indexed entry at keyChain. The optional getValue is a boolean which indicates whether or not to return the removed value in the callback. The callback is in form:

callback(err, value)

get

get(keyChain, callback)

Get the value at keyChain. The callback is in form:

callback(err, value)

getRange

getRange(keyChain, fromIndex,[ toIndex,] callback)

This function assumes that the value at keyChain is an Array or Object. Capture all values starting at fromIndex and finishing at toIndex but not including toIndex. If toIndex is not specified, all values from fromIndex until the end of the Array or Object will be included. The callback is in form:

callback(err, value)

getAll

getAll(callback)

Get all the values in scBroker. The callback is in form:

callback(err, value)

count

count(keyChain, callback)

Count the number of elements at keyChain. The callback is in form:

callback(err, value)

publish subscribe

scBroker provides publish and subscribe functionality.

subscribe

subscribe(channel, ackCallback)

Watch a channel on scBroker. This is the scBroker equivalent to Redis' subscribe(). When an event happens on any watched channel, you can handle it using

scBrokerClient.on('message', function (channel, data) {
    // ...
})

unsubscribe

unsubscribe(channel, ackCallback)

Unwatch the specified channel. If channel is not specified, it will unsubscribe from all channels.

on

on(event, listener)

Listen to events on scBroker, you should listen to the 'message' event to handle messages from subscribed channels. Events are:

  • 'ready': Triggers when scBroker is initialized and connected. You often don't need to wait for that event though. The scBroker client will buffer actions until the scBroker server ready.
  • 'exit' This event carries two arguments to it's listener: code and signal. It gets triggered when the scBroker server process dies.
  • 'connect_failed' This happens if the scBroker client fails to connect to the server after the maximum number of retries have been attempted.
  • 'message' Captures data published to a channel which the client is subscribed to.
  • 'subscribe' Triggers whenever a successful subscribe operations occurs.
  • 'subscribefail' Triggers whenever a subscribtion fails.
  • 'unsubscribe' Triggers on a successful unsubscribe operation.
  • 'unsubscribefail' Triggers whenever a unsubscribtion fails.
  • 'error' Triggers whenever a error occurs.

publish

publish(channel, message, callback)

Publish data to a channel - Can be any JSON-compatible JavaScript object.

Example:

After starting the server (server.js):

var scBroker = require('sc-broker')
  , dss   = scBroker.createServer({port: 9000})

a first client (client1.js) can subscribe to channel foo and listen to messages:

var scBroker = require('sc-broker')
  , dc       = scBroker.createClient({port: 9000})
  , ch       = 'foo'
  , onMsgFn  = function (ch, data) {
      console.log('message on channel ' + ch );
      console.log('data:');
      console.log(data);
    }
dc.subscribe(ch, function (err) {
  if (!err) {
    console.log('client 1 subscribed channel ' + ch  );
  }
})
dc.on('message', onMsgFn )

If a second client (client2.js) publishes a message, the first client will execute the onMsgFn function:

var scBroker  = require('sc-broker')
   , dc    = scBroker.createClient({port: 9000})
   , data  = {a: 'b'}
   , ch    = 'foo';
dc.publish(ch,data , function (err) {
  if (!err) {
    console.log('client 2 published data:');
    console.log(data);
  }
})

Keys

scBroker is very flexible with how you can use keys. It lets you set key chains of any dimension without having to manually create each link in the chain.

A key chain is an array of keys - Each subsequent key in the chain is a child of the previous key. For example, consider the following object:

{'this': {'is': {'a': {'key': 123}}}}

The key chain ['this', 'is', 'a', 'key'] would reference the number 123. The key chain ['this', 'is'] would reference the object {'a': {'key': 123}}, etc.

When you start, scBroker will be empty, but this code is perfectly valid:

dataClient.set(['this', 'is', 'a', 'deep', 'key'], 'Hello world');

In this case, scBroker will create the necessary key chain and set the bottom-level 'key' to 'Hello World'. If you were to call:

dataClient.get(['this', 'is', 'a'], function (err, val) {
    console.log(val);
});

The above would output:

{deep:{key:'Hello world'}}

scBroker generally doesn't restrict you from doing anything you want. Following from the previous example, it is perfectly OK to call this:

dataClient.add(['this', 'is', 'a'], 'foo');

In this case, the key chain ['this', 'is', 'a'] would evaluate to:

{0:'foo', deep:{key:'Hello world'}}

In this case, scBroker will add the value at the next numeric index in the specified key path (which in this case is 0).

You can access numerically-indexed values like this:

dataClient.get(['this', 'is', 'a', 0], function (err, val) {
    console.log(val);
});

The output here will be 'foo'. You can also add entire JSON-compatible objects as value.

Tests

To run tests, go to the sc-broker module directory then run:

npm test

If you get an error, make sure that you have mocha installed:

npm install mocha