@dashersw/axon

中文文档
2.0.5发布于 2017/06/03MIT近 30 天下载 6472

High-level messaging & socket patterns implemented in pure js

原始使用文档
文档目录18 节
  1. Installation
  2. Features
  3. Events
  4. Patterns
  5. Mixed argument types
  6. Push / Pull
  7. Pub / Sub
  8. Req / Rep
  9. PubEmitter / SubEmitter
  10. Socket Options
  11. Binding / Connecting
  12. Protocol
  13. Performance
  14. What's it good for?
  15. Running tests
  16. Authors
  17. Links
  18. License

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.

Build Status

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 closed
  • error (err) when an un-handled socket error occurs
  • ignored error (err) when an axon-handled socket error occurs, but is ignored
  • socket error (err) emitted regardless of handling, for logging purposes
  • reconnect attempt when a reconnection attempt is made
  • connect when connected to the peer, or a peer connection is accepted
  • disconnect when an accepted peer disconnects
  • bind when the server is bound
  • drop (msg) when a message is dropped due to the HWM
  • flush (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.

push.send('image', { w: 100, h: 200 }, imageBuffer);
pull.on('message', function(type, size, img){});

Push / Pull

PushSockets distribute messages round-robin:

var axon = require('axon');
var sock = axon.socket('push');

sock.bind(3000);
console.log('push server started');

setInterval(function(){
  sock.send('hello');
}, 150);

Receiver of PushSocket messages:

var axon = require('axon');
var sock = axon.socket('pull');

sock.connect(3000);

sock.on('message', function(msg){
  console.log(msg.toString());
});

Both PushSockets and PullSockets may .bind() or .connect(). In the following configuration the push socket is bound and pull "workers" connect to it to receive work:

push bind

This configuration shows the inverse, where workers connect to a "sink" to push results:

pull bind

Pub / Sub

PubSockets 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.

var axon = require('axon');
var sock = axon.socket('pub');

sock.bind(3000);
console.log('pub server started');

setInterval(function(){
  sock.send('hello');
}, 500);

SubSocket simply receives any messages from a PubSocket:

var axon = require('axon');
var sock = axon.socket('sub');

sock.connect(3000);

sock.on('message', function(msg){
  console.log(msg.toString());
});

SubSockets may optionally .subscribe() to one or more "topics" (the first multipart value), using string patterns or regular expressions:

var axon = require('axon');
var sock = axon.socket('sub');

sock.connect(3000);
sock.subscribe('user:login');
sock.subscribe('upload:*:progress');

sock.on('message', function(topic, msg){

});

Req / Rep

ReqSocket is similar to a PushSocket in that it round-robins messages to connected RepSockets, however it differs in that this communication is bi-directional, every req.send() must provide a callback which is invoked when the RepSocket replies.

var axon = require('axon');
var sock = axon.socket('req');

sock.bind(3000);

sock.send(img, function(res){

});

RepSockets receive a reply callback that is used to respond to the request, you may have several of these nodes.

var axon = require('axon');
var sock = axon.socket('rep');

sock.connect(3000);

sock.on('message', function(img, reply){
  // resize the image
  reply(img);
});

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:

var axon = require('axon');
var sock = axon.socket('req');

sock.bind(3000);

sock.send('resize', img, function(res){

});

Respond to the "resize" task:

var axon = require('axon');
var sock = axon.socket('rep');

sock.connect(3000);

sock.on('message', function(task, img, reply){
  switch (task) {
    case 'resize':
      // resize the image
      reply(img);
      break;
  }
});

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:

var axon = require('axon');
var sock = axon.socket('pub-emitter');

sock.connect(3000);

setInterval(function(){
  sock.emit('login', { name: 'tobi' });
}, 500);

logger.js:

var axon = require('axon');
var sock = axon.socket('sub-emitter');

sock.bind(3000);

sock.on('user:login', function(user){
  console.log('%s signed in', user.name);
});

sock.on('user:*', function(action, user){
  console.log('%s %s', user.name, action);
});

sock.on('*', function(event){
  console.log(arguments);
});

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]
  • 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

License

MIT

版本与发布记录

@dashersw/axon 共发布 3 个版本,首次发布于 2016/07/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.5latest2017/06/039 年前
2.0.42017/05/289 年前
2.0.32016/07/1210 年前

关于 @dashersw/axon 的常见问题

@dashersw/axon 是什么?

High-level messaging & socket patterns implemented in pure js

如何安装 @dashersw/axon?

在项目目录运行 npm install @dashersw/axon,或使用 pnpm add / yarn add / bun add。

@dashersw/axon 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dashersw__axon 是否存在。

@dashersw/axon 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.11.8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dashersw/axon 可以商用吗?

@dashersw/axon 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dashersw/axon 的下载量有多少?

根据 npm 官方下载统计,@dashersw/axon 在 2026/10/12 – 2026/11/10 这 30 天内被下载 6,472 次,日均约 216 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dashersw/axon 还在维护吗?

最新版本 2.0.5 发布于 2017/06/03(9 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/07/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。