Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 5c2217fdbeeadb70c5f4895175faec0f01a32abb @tj committed Jul 12, 2012
@@ -0,0 +1,2 @@
+node_modules
+testing
@@ -0,0 +1,7 @@
+examples
+<<<<<<< HEAD
+=======
+Readme.md
+test
+testing
+>>>>>>> 9288590b9bb16130c35d64447747328678b0da2b
@@ -0,0 +1,7 @@
+
+TESTS = $(wildcard test/test.*.js)
+
+test:
+ @./test/run $(TESTS)
+
+.PHONY: test
@@ -0,0 +1,176 @@
+
+# Super Sockets
+
+ Super Sockets is a message-oriented socket library for node.js heavily inspired by zeromq.
+
+## Features
+
+ - message oriented
+ - automated reconnection
+ - light-weight wire protocol
+ - light-weight implementation (~300 SLOC)
+ - supports arbitrary binary message (msgpack, json, BLOBS, etc)
+ - supports JSON messages
+ - push / pull pattern
+ - pub / sub pattern
+ - event emitter pattern
+
+## Push / Pull
+
+`PushSocket`s distribute messages round-robin.
+
+```js
+var ss = require('super-sockets');
+var sock = ss.socket('push');
+
+sock.bind(3000);
+console.log('push server started');
+
+setInterval(function(){
+ process.stdout.write('.');
+ sock.send('hello');
+}, 150);
+```
+
+Receiver of `PushSocket` messages.
+
+```js
+var ss = require('../../')
+ , sock = ss.socket('push');
+
+sock.bind(3000);
+console.log('push server started');
+
+setInterval(function(){
+ process.stdout.write('.');
+ sock.send('hello');
+}, 150);
+```
+
+## Pub / Sub
+
+`PubSocket`s send messages to all subscribers without queueing.
+
+_pub.js_:
+
+```js
+var ss = require('super-sockets');
+var sock = ss.socket('pub');
+
+sock.bind(3000);
+console.log('pub server started');
+
+setInterval(function(){
+ console.log('sending');
+ sock.send('hello');
+}, 500);
+```
+
+`SubSocket` provides selective reception of messages from a `PubSocket`.
+
+_sub.js_:
+
+```js
+var ss = require('super-sockets');
+var sock = ss.socket('sub');
+
+sock.connect(3000);
+
+sock.on('message', function(msg){
+ console.log(msg.toString());
+});
+```
+
+## EmitterSocket
+
+`EmitterSocket`'s send and receive messages behaving like regular node `EventEmitter`s.
+This is achieved by using pub / sub sockets behind the scenes. Currently we simply choose
+define the `EmitterSocket` as a `PubSocket` if you `.bind()`, and `SubSocket` if you `.connect()`,
+providing the natural API you're used to:
+
+server.js:
+
+```js
+var ss = require('super-sockets');
+var sock = ss.socket('emitter');
+
+sock.bind(3000);
+console.log('pub server started');
+
+setInterval(function(){
+ sock.emit('login', { name: 'tobi' });
+}, 500);
+```
+
+client.js:
+
+```js
+var ss = require('super-sockets');
+var sock = ss.socket('emitter');
+
+sock.connect(3000);
+console.log('sub client connected');
+
+sock.on('login', function(user){
+ console.log('%s signed in', user.name);
+});
+```
+
+## Protocol
+
+ The wire protocol is simple and very much zeromq-like, where `<length>` is
+ a 24 bit unsigned integer, representing a maximum length of 2^24, roughly ~16mb,
+ so you should be good :p
+
+```
+ octet: 0 1 2 3 <length>
+ +------+------+------+------+------------------...
+ | meta | <length> | data ...
+ +------+------+------+------+------------------...
+```
+
+## Running tests
+
+```
+$ npm install
+$ make test
+```
+
+## Todo
+
+ - more tests
+ - code cov
+ - acks
+ - emitter style on top of pubsub with multipart
+ - pluggable codecs
+ - weighted fair queuing
+ - use mocha for tests
+ - multipart
+ - subscriptions
+ - ...
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2012 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
+
+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.
+
@@ -0,0 +1,10 @@
+
+var zmq = require('..')
+ , pub = zmq.socket('pub');
+
+var n = 1000;
+
+pub.bind(3000, function(){
+ console.log('bound to :3000');
+ while (n--) pub.send('foo');
+});
@@ -0,0 +1,26 @@
+
+var zmq = require('..')
+ , sub = zmq.socket('sub');
+
+var start = new Date
+ , n = 1000
+ , ops = n;
+
+sub.on('message', function(msg){
+ console.error(msg);
+ --n || (function(){
+ var duration = new Date - start;
+ pub.close();
+ sub.close();
+ console.log();
+ console.log(' pub/sub:');
+ console.log(' \033[36m%d\033[m msgs', ops);
+ console.log(' \033[36m%d\033[m ops/s', ops / (duration / 1000) | 0);
+ console.log(' \033[36m%d\033[m ms', duration);
+ console.log();
+ })();
+});
+
+sub.connect(3000, function(){
+ console.log('connected to :3000');
+});
@@ -0,0 +1,14 @@
+
+var ss = require('../..')
+ , sock = ss.socket('emitter');
+
+sock.connect(3000);
+console.log('emitter client connected');
+
+sock.on('login', function(user){
+ console.log('%s signed in', user.name);
+});
+
+sock.on('logout', function(user){
+ console.log('%s signed out', user.name);
+});
@@ -0,0 +1,14 @@
+
+var ss = require('../..')
+ , sock = ss.socket('emitter');
+
+sock.bind(3000);
+console.log('emitter server started');
+
+setInterval(function(){
+ sock.emit('login', { name: 'tobi' });
+}, 300);
+
+setInterval(function(){
+ sock.emit('logout', { name: 'tobi' });
+}, 1500);
@@ -0,0 +1,11 @@
+
+var ss = require('../..')
+ , sock = ss.socket('pub');
+
+sock.bind(3000);
+console.log('pub server started');
+
+setInterval(function(){
+ console.log('sending');
+ sock.send('hello');
+}, 500);
@@ -0,0 +1,9 @@
+
+var ss = require('../..')
+ , sock = ss.socket('sub');
+
+sock.connect(3000);
+
+sock.on('message', function(msg){
+ console.log(msg.toString());
+});
@@ -0,0 +1,9 @@
+
+var ss = require('../..')
+ , sock = ss.socket('pull');
+
+sock.connect(3000);
+
+sock.on('message', function(msg){
+ console.log(msg.toString());
+});
@@ -0,0 +1,11 @@
+
+var ss = require('../..')
+ , sock = ss.socket('push');
+
+sock.bind(3000);
+console.log('push server started');
+
+setInterval(function(){
+ process.stdout.write('.');
+ sock.send('hello');
+}, 150);
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib');
@@ -0,0 +1,18 @@
+
+/**
+ * Binary.
+ */
+
+exports.none = {
+ encode: function(msg){ return msg },
+ decode: function(msg){ return msg }
+};
+
+/**
+ * JSON.
+ */
+
+exports.json = {
+ encode: JSON.stringify,
+ decode: JSON.parse
+};
Oops, something went wrong.

0 comments on commit 5c2217f

Please sign in to comment.