Permalink
Browse files

added Node.JS Discovery Service powered by PubNub provided by "Yarosl…

…av Gaponov" who provided the nifty WINSAY.js for node.js powered by PubNub.
  • Loading branch information...
stephenlb committed Apr 6, 2012
1 parent f192573 commit fbfe512387f5f132b0a06753d35084e3b7ee7105
@@ -20,7 +20,7 @@
<div id=audio-mosaic>Audio Mosaic</div>
<div id=userlist></div>
-<div id=pubnub></div>
+<div id=pubnub pub-key=demo sub-key=demo></div>
<script src=http://cdn.pubnub.com/pubnub-3.1.js></script>
<script src=audio-mosaic.js></script>
</div></body>
@@ -0,0 +1 @@
+./loader.js discovery
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var helper = require('./../lib/helper.js');
+
+var pubnub = require("./../lib/pubnub.js").init({
+ publish_key : "demo",
+ subscribe_key : "demo"
+});
+
+
+helper.query(pubnub, 'echo', 'Hello world', function(response) {
+ if (response) {
+ if (response.responseSet)
+ console.log("response: " + response.responseSet);
+ if (response.errorSet)
+ console.log("error: " + response.responseSet);
+ }
+ process.exit();
+});
+
+
+
+
+
@@ -0,0 +1,26 @@
+#!/usr/bin/env node
+
+var uuid = require('./../lib/uuid.js'),
+ helper = require('./../lib/helper.js');
+
+var pubnub = require("./../lib/pubnub.js").init({
+ publish_key : "demo",
+ subscribe_key : "demo"
+});
+
+
+
+helper.getServiceList(pubnub, function(err, services) {
+ if (!err) {
+ for(var i = 0; i < services.list.length; i++) {
+ console.log(services.list[i]);
+ }
+ process.exit();
+ }
+});
+
+
+
+
+
+
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var helper = require('./../lib/helper.js');
+
+var pubnub = require("./../lib/pubnub.js").init({
+ publish_key : "demo",
+ subscribe_key : "demo"
+});
+
+
+helper.query(pubnub, 'time', null, function(response) {
+ if (response) {
+ if (response.responseSet)
+ console.log("response: " + response.responseSet);
+ if (response.errorSet)
+ console.log("error: " + response.responseSet);
+ }
+ process.exit();
+});
+
+
+
+
+
@@ -0,0 +1,30 @@
+#!/usr/bin/env node
+
+var helper = require('./../lib/helper.js');
+
+var pubnub = require("./../lib/pubnub.js").init({
+ publish_key : "demo",
+ subscribe_key : "demo"
+});
+
+var request = {
+ "appId": "E7A859ECBE5107A9C7DF91B1B450F6096212ED7B",
+ "from": "en",
+ "to": "it",
+ "text": "I'll be back."
+};
+
+helper.query(pubnub, 'translate', request, function(response) {
+ if (response) {
+ if (response.responseSet)
+ console.log("response: %s -> %s", request.to, response.responseSet);
+ if (response.errorSet)
+ console.log("error: " + response.responseSet);
+ }
+ process.exit();
+});
+
+
+
+
+
@@ -0,0 +1,8 @@
+{
+ "interval": "-1",
+ "delayBeforeShutdown": "3000",
+ "pubnub": {
+ "publish_key": "demo",
+ "subscribe_key": "demo"
+ }
+}
@@ -0,0 +1,2 @@
+
+module.exports.channel = '2ed55452-64b8-4ddd-812c-264679836197';
@@ -0,0 +1,125 @@
+
+var uuid = require('./../lib/uuid.js'),
+ discovery = require('./discovery.js');
+
+module.exports.getServiceChannel = function(server, name, callback) {
+ var client_channel = uuid().toString();
+
+ server.subscribe({
+ channel : client_channel,
+ callback : function(message) {
+ if ((!message) || (!message.responseSet)) return callback(message.errorSet, null)
+ else return callback(null, message.responseSet);
+ }
+ });
+
+ server.publish({
+ channel : discovery.channel,
+ message : {
+ clientChannel: client_channel,
+ requestSet: {
+ op: "get",
+ name: name
+ }
+ }
+ })
+}
+
+module.exports.getServiceList = function(server, callback) {
+ var client_channel = uuid().toString();
+
+ server.subscribe({
+ channel : client_channel,
+ callback : function(message) {
+ if ((! message) || (! message.responseSet)) return callback(message.errorSet, null)
+ else return callback(null, message.responseSet);
+ }
+ });
+
+ server.publish({
+ channel : discovery.channel,
+ message : {
+ clientChannel: client_channel,
+ requestSet: {
+ op: "list"
+ }
+ }
+ })
+}
+
+
+module.exports.getServiceChannel = function(server, name, callback) {
+ var client_channel = uuid().toString();
+
+ server.subscribe({
+ channel : client_channel,
+ callback : function(message) {
+ if ((!message) || (!message.responseSet)) return callback(message.errorSet, null)
+ else return callback(null, message.responseSet);
+ }
+ });
+
+ server.publish({
+ channel : discovery.channel,
+ message : {
+ clientChannel: client_channel,
+ requestSet: {
+ op: "get",
+ name: name
+ }
+ }
+ })
+}
+
+
+module.exports.registryServiceChannel = function(server, name, channel) {
+ server.publish({
+ channel : discovery.channel,
+ message : {
+ requestSet:{
+ op: 'set',
+ name: name,
+ channel: channel
+ }
+ }
+ })
+};
+
+module.exports.unregistryServiceChannel = function(server, name, channel) {
+ server.publish({
+ channel : discovery.channel,
+ message : {
+ requestSet:{
+ op: 'del',
+ name: name,
+ channel: channel
+ }
+ }
+ })
+};
+
+
+
+module.exports.query = function(server, service, request, callback) {
+ var client_channel = uuid().toString();
+
+ server.subscribe({
+ channel : client_channel,
+ callback : function(response) {
+ return callback(response);
+ }
+ });
+
+ this.getServiceChannel(server, service, function(err, channel) {
+ if (err) throw err;
+ server.publish({
+ channel : channel,
+ message : {
+ clientChannel: client_channel,
+ requestSet: request
+ }
+ });
+ });
+};
+
+
Oops, something went wrong.

0 comments on commit fbfe512

Please sign in to comment.