Permalink
Browse files

add better HWM example

  • Loading branch information...
tj committed Jan 1, 2013
1 parent b8ff0db commit e24aadd8e554c1c50badbc1416253a0df29b5ce4
Showing with 29 additions and 31 deletions.
  1. +29 −31 examples/hwm/index.js
View
@@ -6,45 +6,43 @@ var axon = require('../..')
// by default the high water mark (HWM)
// is Infinity, allowing the queue to
// grow unbounded. Here it is manually
-// set to 20 for demonstration purposes
+// set to 3 for demonstration purposes only
push.connect(3000);
-push.set('hwm', 20);
+push.set('hwm', 3);
-// the receiver (pull socket) periodically
-// unbinds to simulate a poor client
+// faux database as intermediate queue,
+// this could be mongo, disk, anything you want
-setInterval(function(){
- console.log('unbind');
- pull.close();
- setTimeout(function(){
- console.log('bind');
- pull.bind(3000);
- }, 250);
-}, 2000);
-
-// send more messages than
-// the client can handle
-
-var id = 0;
-setInterval(function(){
- push.send(String(++id));
-}, 10);
+var db = [];
+push.on('drop', function(msg){
+ console.log('dropped %s', msg);
+ db.push(msg);
+});
-// received messasges
+push.on('flush', function(msgs){
+ console.log('flushed %d', msgs.length);
+ db.forEach(function(msg){
+ push.send(msg);
+ });
+});
pull.on('message', function(msg){
console.log('recv %s', msg);
});
-// dropped messages
-
-push.on('drop', function(msg){
- console.log('drop %s', msg);
-});
-
-// flushed messages
-
-push.on('flush', function(msgs){
- console.log('flush %d msgs', msgs.length);
+pull.bind(3000, function(){
+ push.send('1');
+ push.send('2');
+ push.send('3');
+ push.send('4');
+ push.send('5');
+ pull.close(function(){
+ push.send('6');
+ push.send('7');
+ push.send('8');
+ push.send('9');
+ push.send('10');
+ pull.bind(3000);
+ });
});

0 comments on commit e24aadd

Please sign in to comment.