Installations
npm install pm2-axon
Developer
Unitech
Developer Guide
Module System
Unable to determine the module system for this package.
Min. Node Version
>=5
Typescript Support
No
Node Version
15.4.0
NPM Version
7.0.15
Statistics
21 Stars
394 Commits
10 Forks
6 Watching
2 Branches
16 Contributors
Updated on 19 Oct 2024
Languages
JavaScript (99.02%)
Makefile (0.66%)
Shell (0.32%)
Total Downloads
Cumulative downloads
Total Downloads
371,310,203
Last day
-1.6%
259,068
Compared to previous day
Last week
2.2%
1,506,513
Compared to previous week
Last month
5.2%
6,265,876
Compared to previous month
Last year
-33.4%
77,482,850
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
Dev Dependencies
5
Axon
Axon is a message-oriented socket library for node.js heavily inspired by zeromq. For a light-weight UDP alternative you may be interested in punt.
Installation
$ npm install axon
Features
- message oriented
- automated reconnection
- light-weight wire protocol
- mixed-type arguments (strings, objects, buffers, etc)
- unix domain socket support
- fast (~800 mb/s ~500,000 messages/s)
Events
close
when server or connection is closederror
(err) when an un-handled socket error occursignored error
(err) when an axon-handled socket error occurs, but is ignoredsocket error
(err) emitted regardless of handling, for logging purposesreconnect attempt
when a reconnection attempt is madeconnect
when connected to the peer, or a peer connection is accepteddisconnect
when an accepted peer disconnectsbind
when the server is bounddrop
(msg) when a message is dropped due to the HWMflush
(msgs) queued when messages are flushed on connection
Patterns
- push / pull
- pub / sub
- req / rep
- pub-emitter / sub-emitter
Mixed argument types
Backed by node-amp-message you may pass strings, objects, and buffers as arguments.
1push.send('image', { w: 100, h: 200 }, imageBuffer); 2pull.on('message', function(type, size, img){});
Push / Pull
PushSocket
s distribute messages round-robin:
1var axon = require('axon'); 2var sock = axon.socket('push'); 3 4sock.bind(3000); 5console.log('push server started'); 6 7setInterval(function(){ 8 sock.send('hello'); 9}, 150);
Receiver of PushSocket
messages:
1var axon = require('axon'); 2var sock = axon.socket('pull'); 3 4sock.connect(3000); 5 6sock.on('message', function(msg){ 7 console.log(msg.toString()); 8});
Both PushSocket
s and PullSocket
s may .bind()
or .connect()
. In the
following configuration the push socket is bound and pull "workers" connect
to it to receive work:
This configuration shows the inverse, where workers connect to a "sink" to push results:
Pub / Sub
PubSocket
s send messages to all subscribers without queueing. This is an
important difference when compared to a PushSocket
, where the delivery of
messages will be queued during disconnects and sent again upon the next connection.
1var axon = require('axon'); 2var sock = axon.socket('pub'); 3 4sock.bind(3000); 5console.log('pub server started'); 6 7setInterval(function(){ 8 sock.send('hello'); 9}, 500);
SubSocket
simply receives any messages from a PubSocket
:
1var axon = require('axon'); 2var sock = axon.socket('sub'); 3 4sock.connect(3000); 5 6sock.on('message', function(msg){ 7 console.log(msg.toString()); 8});
SubSocket
s may optionally .subscribe()
to one or more "topics" (the first multipart value),
using string patterns or regular expressions:
1var axon = require('axon'); 2var sock = axon.socket('sub'); 3 4sock.connect(3000); 5sock.subscribe('user:login'); 6sock.subscribe('upload:*:progress'); 7 8sock.on('message', function(topic, msg){ 9 10});
Req / Rep
ReqSocket
is similar to a PushSocket
in that it round-robins messages
to connected RepSocket
s, however it differs in that this communication is
bi-directional, every req.send()
must provide a callback which is invoked
when the RepSocket
replies.
1var axon = require('axon'); 2var sock = axon.socket('req'); 3 4sock.bind(3000); 5 6sock.send(img, function(res){ 7 8});
RepSocket
s receive a reply
callback that is used to respond to the request,
you may have several of these nodes.
1var axon = require('axon'); 2var sock = axon.socket('rep'); 3 4sock.connect(3000); 5 6sock.on('message', function(img, reply){ 7 // resize the image 8 reply(img); 9});
Like other sockets you may provide multiple arguments or an array of arguments, followed by the callbacks. For example here we provide a task name of "resize" to facilitate multiple tasks over a single socket:
1var axon = require('axon'); 2var sock = axon.socket('req'); 3 4sock.bind(3000); 5 6sock.send('resize', img, function(res){ 7 8});
Respond to the "resize" task:
1var axon = require('axon'); 2var sock = axon.socket('rep'); 3 4sock.connect(3000); 5 6sock.on('message', function(task, img, reply){ 7 switch (task) { 8 case 'resize': 9 // resize the image 10 reply(img); 11 break; 12 } 13});
PubEmitter / SubEmitter
PubEmitter
and SubEmitter
are higher-level Pub
/ Sub
sockets, using the "json" codec to behave much like node's EventEmitter
. When a SubEmitter
's .on()
method is invoked, the event name is .subscribe()
d for you. Each wildcard (*
) or regexp capture group is passed to the callback along with regular message arguments.
app.js:
1var axon = require('axon'); 2var sock = axon.socket('pub-emitter'); 3 4sock.connect(3000); 5 6setInterval(function(){ 7 sock.emit('login', { name: 'tobi' }); 8}, 500);
logger.js:
1var axon = require('axon'); 2var sock = axon.socket('sub-emitter'); 3 4sock.bind(3000); 5 6sock.on('user:login', function(user){ 7 console.log('%s signed in', user.name); 8}); 9 10sock.on('user:*', function(action, user){ 11 console.log('%s %s', user.name, action); 12}); 13 14sock.on('*', function(event){ 15 console.log(arguments); 16});
Socket Options
Every socket has associated options that can be configured via get/set
.
identity
- the "name" of the socket that uniqued identifies it.retry timeout
- connection retry timeout in milliseconds [100] (0 = do not reconnect)retry max timeout
- the cap for retry timeout length in milliseconds [5000]hwm
- the high water mark threshold for queues [Infinity]
Binding / Connecting
In addition to passing a portno, binding to INADDR_ANY by default, you
may also specify the hostname via .bind(port, host)
, another alternative
is to specify the url much like zmq via tcp://<hostname>:<portno>
, thus
the following are equivalent:
sock.bind(3000)
sock.bind(3000, '0.0.0.0')
sock.bind('tcp://0.0.0.0:3000')
sock.connect(3000)
sock.connect(3000, '0.0.0.0')
sock.connect('tcp://0.0.0.0:3000')
You may also use unix domain sockets:
sock.bind('unix:///some/path')
sock.connect('unix:///some/path')
Protocol
Axon 2.x uses the extremely simple AMP protocol to send messages on the wire. Codecs are no longer required as they were in Axon 1.x.
Performance
Preliminary benchmarks on my Macbook Pro based on 10 messages per tick as a realistic production application would likely have even less than this. "better" numbers may be acheived with batching and a larger messages/tick count however this is not realistic.
64 byte messages:
min: 47,169 ops/s
mean: 465,127 ops/s
median: 500,000 ops/s
total: 2,325,636 ops in 5s
through: 28.39 mb/s
1k messages:
min: 48,076 ops/s
mean: 120,253 ops/s
median: 121,951 ops/s
total: 601,386 ops in 5.001s
through: 117.43 mb/s
8k messages:
min: 36,496 ops/s
mean: 53,194 ops/s
median: 50,505 ops/s
total: 266,506 ops in 5.01s
through: 405.84 mb/s
32k messages:
min: 12,077 ops/s
mean: 14,792 ops/s
median: 16,233 ops/s
total: 74,186 ops in 5.015s
through: 462.28 mb/s
What's it good for?
Axon are not meant to combat zeromq nor provide feature parity, but provide a nice solution when you don't need the insane nanosecond latency or language interoperability that zeromq provides as axon do not rely on any third-party compiled libraries.
Running tests
$ npm install
$ make test
Authors
Links
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/21 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More