Metroplex a Redis based spark/connection registry for Primus.
Metroplex is released in the npm registry and can therefor be installed using:
npm install --save metroplex
Once you've installed the module you need to tell Primus to use the plugin which
is done using the
'use strict';var http =Primus =primus = http transformer: 'sockjs' ;primus;
In the example above you've seen how to add the plugin to your Primus server but not how to configure it. We have various of options that can be configured in this plugin:
- redis: Metroplex is currently using Redis as its default back-end for storing
the state of the connections. If you do not supply us with a pre-defined Redis
client (or authorized) we will create a Redis client which only connects to
localhost and Redis's default port number. When provided this must be an
- namespace: As the databases are usually shared with other programs it's good
to prefix all the data that you store, in Metroplex we prefix every key with
the set namespace. The default namespace is
- interval: We are using "alive" suffixed keys in the database to see which
node process is still alive. The interval determines the interval of these
updates. When the interval is reached we update the key in the database with
the current EPOCH as well as start a scan for possible dead servers and
removing them. The default interval
- latency: The maximum time it would take to update the
alivekey in Redis. This time is subtracted from the set
intervalso we update the key BEFORE it expires. Defaults to
- address The address or public URL on which this SPECIFIC server is
reachable. Should be without path name. When nothing is supplied we try to be
somewhat smart and read the address and port and server type from the server
that Primus is attached to and compose an URL like:
These options should be provided in the options object of the Primus server:
primus = httptransformer: 'sockjs'namespace: 'metroplex'redis:;primus;
The orchestration is all done using the
metroplex library which is bundled in
this plugin. The Metroplex instance is exposed on the
Primus instance when you
use this plugin:
The following public methods are available.
List all the servers in our current registry.
Get the server for the given spark id. It does not check if the spark is hosted on the current server. That's up to the developer to implement.
Get the servers for each id in the given
sparks array. It will return an
object and just like
metroplex.spark it does not check if the spark is hosted
on the current server.
Omega Supreme integration
If you load the
metroplex, you can use some additional convenience methods.
These methods are added to the
Broadcast a message to all sparks in the cluster.
forward.sparks(ids, msg, fn)
Send a message to a set of sparks in the cluster.
forward.spark(id, msg, fn)
Send a message to a single spark in the cluster.