Permalink
Browse files

First commit

  • Loading branch information...
1 parent b6aa99b commit 2fcb6d4b59430c695ccc3633a50b5f427b5cd432 @morphar morphar committed Mar 11, 2013
Showing with 119 additions and 3 deletions.
  1. +19 −0 LICENSE
  2. +17 −3 README.md
  3. +32 −0 gevents.js
  4. +36 −0 package.json
  5. +15 −0 test.js
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Dan Larsen
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
20 README.md
@@ -1,4 +1,18 @@
-gevents
-=======
+# gevents
-An extension to node.js events, that adds global events through redis
+Extends the native node.js events with a global emitter through [Redis](http://redis.io).
+
+The single purpose is to add the ability to emit an event, that is transported through [Redis](http://redis.io), making the event "global" to all processes and computers, that are listening with gevents on the same [Redis](http://redis.io) setup.
+
+### This is an early BETA version
+
+As soon as the module has shown it's worth and stability on a live system, it will be marked as version >= 1.0.0.
+
+Until then: Feel free to play around with it, learn from it.
+
+### To install
+
+ npm install gevents
+
+### Todo
+Add an options argument that can contain connection info etc.
View
32 gevents.js
@@ -0,0 +1,32 @@
+var util = require('util');
+var events = require('events');
+var redis = require('redis');
+
+var redisPub = redis.createClient();
+var redisSub = redis.createClient();
+
+var GEvents = function(globalPrefix) {
+ events.EventEmitter.call(this);
+
+ this.globalPrefix = globalPrefix || 'gevents_'
+ this.setMaxListeners(100);
+ var self = this;
+ redisSub.on('pmessage', function(channelPattern, channel, message) {
+ channel = channel.substr(self.globalPrefix.length);
+ self.emit(channel, message);
+ });
+ redisSub.psubscribe(this.globalPrefix+'*');
+};
+util.inherits(GEvents, events.EventEmitter);
+
+GEvents.prototype.globalEmit = function() {
+ if(arguments.length === 0) {
+ return false;
+ } else {
+ var channel = this.globalPrefix + arguments[0];
+ delete arguments[0];
+ redisPub.publish(channel, JSON.stringify(arguments));
+ }
+};
+
+module.exports = new GEvents();
View
36 package.json
@@ -0,0 +1,36 @@
+{
+ "name": "gevents",
+ "version": "0.1.0",
+ "description": "An extension to node.js events, that adds global events through redis",
+ "keywords":[
+ "events",
+ "global",
+ "redis"
+ ],
+ "author": "Dan Larsen",
+ "maintainers": [{
+ "name": "Dan Larsen",
+ "email": "dan@techba.se",
+ "url": "http://github.com/morphar"
+ }],
+ "dependencies": {
+ "redis": ">=0.8.2",
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/morphar/gevents.git"
+ },
+ "github": "http://github.com/morphar/gevents",
+ "bugs": {
+ "mail": "",
+ "url": "http://github.com/morphar/gevents/issues"
+ },
+ "license": {
+ "type": "MIT",
+ "url": "http://github.com/morphar/gevents/blob/master/LICENSE"
+ },
+ "main": "./index",
+ "engines": {
+ "node": ">=0.8.0"
+ }
+}
View
15 test.js
@@ -0,0 +1,15 @@
+var util = require('util');
+var GEvents1 = require('./gevents.js');
+var GEvents2 = require('./gevents.js');
+
+GEvents1.on('bla', function() { console.log('bla', arguments); });
+
+setInterval(function() {
+
+ if(!(process.pid % 2)) {
+ GEvents2.emit('bla', { some: 'data' }, 'bla', 'yadda');
+ } else {
+ GEvents2.globalEmit('bla', { some: 'data2' });
+ }
+
+ }, 2000);

0 comments on commit 2fcb6d4

Please sign in to comment.