Permalink
Browse files

Added more examples

  • Loading branch information...
utvara authored and tj committed Mar 6, 2012
1 parent d450ecc commit 8a9d345fce52082b77d46afa1c3c56eeb3f8a46f
Showing with 320 additions and 0 deletions.
  1. +54 −0 examples/dealer_router.js
  2. +55 −0 examples/pub_sub.js
  3. +48 −0 examples/push_pull.js
  4. +53 −0 examples/rep_req.js
  5. +57 −0 examples/req_rep.js
  6. +53 −0 examples/router_dealer.js
View
@@ -0,0 +1,54 @@
+/*
+ *
+ * One client two servers (round roobin)
+ *
+ */
+
+var cluster = require('cluster')
+ , zmq = require('../')
+ , port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ for (var i = 0; i < 2; i++) cluster.fork();
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //dealer = client
+
+ var socket = zmq.socket('dealer');
+
+ socket.identity = 'client' + process.pid;
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ setInterval(function() {
+ var value = Math.floor(Math.random()*100);
+
+ console.log(socket.identity + ': asking ' + value);
+ socket.send(value);
+ }, 100);
+
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': answer data ' + data);
+ });
+ });
+} else {
+ //router = server
+
+ var socket = zmq.socket('router');
+
+ socket.identity = 'server' + process.pid;
+
+ socket.connect(port);
+ console.log('connected!');
+
+ socket.on('message', function(envelope, data) {
+ console.log(socket.identity + ': received ' + envelope + ' - ' + data.toString());
+ socket.send([envelope, data * 2]);
+ });
+}
View
@@ -0,0 +1,55 @@
+/*
+ *
+ * Publisher subscriber pattern
+ *
+ */
+
+var cluster = require('cluster')
+ , zmq = require('../')
+ , port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ for (var i = 0; i < 2; i++) cluster.fork();
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //publisher = send only
+
+ var socket = zmq.socket('pub');
+
+ socket.identity = 'publisher' + process.pid;
+
+ var stocks = ['AAPL', 'GOOG', 'YHOO', 'MSFT', 'INTC'];
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ setInterval(function() {
+ var symbol = stocks[Math.floor(Math.random()*stocks.length)]
+ , value = Math.random()*1000;
+
+ console.log(socket.identity + ': sent ' + symbol + ' ' + value);
+ socket.send(symbol + ' ' + value);
+ }, 100);
+ });
+} else {
+ //subscriber = receive only
+
+ var socket = zmq.socket('sub');
+
+ socket.identity = 'subscriber' + process.pid;
+
+ socket.connect(port);
+
+ socket.subscribe('AAPL');
+ socket.subscribe('GOOG');
+
+ console.log('connected!');
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': received data ' + data.toString());
+ });
+}
View
@@ -0,0 +1,48 @@
+/*
+ *
+ * Pipeline
+ *
+ */
+
+var cluster = require('cluster')
+ , zmq = require('../')
+ , port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ for (var i = 0; i < 2; i++) cluster.fork();
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //push = upstream
+
+ var socket = zmq.socket('push');
+
+ socket.identity = 'upstream' + process.pid;
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ setInterval(function() {
+ var date = new Date();
+
+ console.log(socket.identity + ': sending data ' + date.toString());
+ socket.send(date.toString());
+ }, 500);
+ });
+} else {
+ //pull = downstream
+
+ var socket = zmq.socket('pull');
+
+ socket.identity = 'downstream' + process.pid;
+
+ socket.connect(port);
+ console.log('connected!');
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': received data ' + data.toString());
+ });
+}
View
@@ -0,0 +1,53 @@
+/*
+ *
+ * One responseder two requesters
+ *
+ */
+
+var cluster = require('cluster')
+ , zmq = require('../')
+ , port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ for (var i = 0; i < 2; i++) cluster.fork();
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //responseder = server
+
+ var socket = zmq.socket('rep');
+
+ socket.identity = 'server' + process.pid;
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': received ' + data.toString());
+ socket.send(2 * data);
+ });
+ });
+} else {
+ //requester = client
+
+ var socket = zmq.socket('req');
+
+ socket.identity = 'client' + process.pid;
+
+ socket.connect(port);
+ console.log('connected!');
+
+ setInterval(function() {
+ var value = Math.floor(Math.random()*100);
+
+ socket.send(value);
+ console.log(socket.identity + ': asking ' + value);
+ }, 100);
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': answer data ' + data);
+ });
+}
View
@@ -0,0 +1,57 @@
+/*
+ *
+ * One requester two responders (round robin)
+ *
+ */
+
+var cluster = require('cluster'),
+ zeromq = require('zmq'),
+ port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ //Fork servers.
+ for (var i = 0; i < 2; i++) {
+ cluster.fork();
+ }
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //requester = client
+
+ var socket = zeromq.socket('req');
+
+ socket.identity = 'client' + process.pid;
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ setInterval(function() {
+ var value = Math.floor(Math.random()*100);
+
+ console.log(socket.identity + ': asking ' + value);
+ socket.send(value);
+ }, 100);
+
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': answer data ' + data);
+ });
+ });
+} else {
+ //responder = server
+
+ var socket = zeromq.socket('rep');
+
+ socket.identity = 'server' + process.pid;
+
+ socket.connect(port);
+ console.log('connected!');
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': received ' + data.toString());
+ socket.send(data * 2);
+ });
+}
View
@@ -0,0 +1,53 @@
+/*
+ *
+ * One server two clients
+ *
+ */
+
+var cluster = require('cluster')
+ , zeromq = require('../')
+ , port = 'tcp://127.0.0.1:12345';
+
+if (cluster.isMaster) {
+ for (var i = 0; i < 2; i++) cluster.fork();
+
+ cluster.on('death', function(worker) {
+ console.log('worker ' + worker.pid + ' died');
+ });
+
+ //router = server
+
+ var socket = zeromq.socket('router');
+
+ socket.identity = 'server' + process.pid;
+
+ socket.bind(port, function(err) {
+ if (err) throw err;
+ console.log('bound!');
+
+ socket.on('message', function(envelope, data) {
+ console.log(socket.identity + ': received ' + envelope + ' - ' + data.toString());
+ socket.send([envelope, data * 2]);
+ });
+ });
+} else {
+ //dealer = client
+
+ var socket = zeromq.socket('dealer');
+
+ socket.identity = 'client' + process.pid;
+
+ socket.connect(port);
+ console.log('connected!');
+
+ setInterval(function() {
+ var value = Math.floor(Math.random()*100);
+
+ socket.send(value);
+ console.log(socket.identity + ': asking ' + value);
+ }, 100);
+
+ socket.on('message', function(data) {
+ console.log(socket.identity + ': answer data ' + data);
+ });
+}

0 comments on commit 8a9d345

Please sign in to comment.