Skip to content

Instantly share code, notes, and snippets.

@crodas
Forked from gleicon/zemitter.js
Created June 9, 2011 03:50
Show Gist options
  • Save crodas/1016019 to your computer and use it in GitHub Desktop.
Save crodas/1016019 to your computer and use it in GitHub Desktop.
ZeroMQ and Node.js pub/sub emitter
#!/bin/bash -x
# crodas
# get code from git
if [[ ! -d node ]]
then
git clone git://github.com/joyent/node
fi
cd node/
git pull origin master
# install required libraries for ubuntu
apt-get install -y build-essential libssl-dev curl
# compile node
./configure
make -j 4
make install
# install npm
curl http://npmjs.org/install.sh | sh
#!/bin/bash -x
#crodas
# get
if [[ ! -f zeromq-2.1.7.tar.gz ]]
then
wget http://download.zeromq.org/zeromq-2.1.7.tar.gz
fi
tar xfvz zeromq-2.1.7.tar.gz
cd zeromq-2.1.7
#
apt-get install - -yy uuid-dev libev-dev
# configure && compile
./configure --with-pgm
make -j 4
make install
# node
npm install zeromq
# php
pear channel-discover pear.zero.mq
pecl install zero.mq/zmq-beta
echo "extension=zmq.so" > /etc/php5/conf.d/zmq.ini
/etc/init.d/apache2 reload
// ZeroMQ PUB/SUB + Multicast (PGM) enabled event emitter for node.js
// gleicon - 2011
var util = require("util");
var events = require("events");
var zeromq = require("zeromq"); // check the right path
var sys = require("sys");
var address = "epgm://192.168.2.7:9999";
function DistEventEmitter(name, remote_node) {
events.EventEmitter.call(this);
this.id = new Date().getTime() + "-" + process.pid;
this.pub_socket = zeromq.createSocket('pub');
this.sub_socket = zeromq.createSocket('sub');
this.pub_socket.connect(address, function(e){ if (e) console.log(e); });
this.sub_socket.connect(address, function(e){ if (e) console.log(e); });
this.sub_socket.subscribe("EVENTS");
this.sub_socket.on("message", DistEventEmitter.prototype._dispatch);
}
util.inherits(DistEventEmitter, events.EventEmitter);
DistEventEmitter.prototype._emit_local = DistEventEmitter.prototype.emit
DistEventEmitter.prototype._dispatch = function (data) {
DistEventEmitter.call(this);
try {
d = JSON.parse(data.toString('utf8').replace('EVENTS ', ''));
console.log('r: '+d.msg)
if (d.id != this.id) DistEventEmitter.prototype._emit_local(d.event_name, d.msg);
console.log('rec: '+d.msg);
} catch (e) { console.log(e); }
}
DistEventEmitter.prototype._broadcast = function(w) {
DistEventEmitter.call(this);
pkt = JSON.stringify({ "id": this.id,
"event_emitter": "data_ws",
"event_name" : "data",
"msg": w });
this.pub_socket.send("EVENTS " + pkt);
}
DistEventEmitter.prototype.emit = function(evt, data) {
this._broadcast(data)
return this._emit_local(evt, data);
}
// test
var de = new DistEventEmitter();
de.on("data", function(data) { console.log('Received data: "' + data + '"'); })
de.on("error", function(data) { console.log('Received data (Error): "' + data + '"'); })
de.emit("data", "It works!")
de.emit("data", "It works 2")
@gleicon
Copy link

gleicon commented Jun 11, 2011

great stuff here, I've just freeze my VM and my mac because I've picked the wrong interface and zeromq had an assert error heh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment