Permalink
Browse files

alpha. triggers working on different channels.

  • Loading branch information...
1 parent 5243331 commit 258e3f71fe6875bc64f389f959e17a0954ef1b77 @daraosn committed May 6, 2012
Showing with 90 additions and 57 deletions.
  1. +6 −4 README.md
  2. +16 −13 mustekala.js
  3. +38 −31 public/example.html
  4. +30 −9 public/mustekala.js
View
@@ -3,9 +3,10 @@
Mustekala
======
-Real-time Push/Pull notification service made out of node.js, using socket.io.
-It lets you execute javascript from the server side to all clients subscribed to a channel.
-You can send commands on any programming language that supports POST requests.
+Real-time Push/Pull notification service built over socket.io/express/nodeJS.
+It lets you subscribe events which can be triggered from the server side to
+every client subscribed to a channel.
+Supports POST triggers, so it is possible to use any language on the server-side.
Example 1
-----
@@ -146,9 +147,10 @@ Contributors
------------
Diego Araos
+
Herman Junge
License
-----
-Mustekala is under MIT License. See the License file.
+Mustekala is under MIT License. See LICENSE file.
View
@@ -76,27 +76,28 @@ var socket=io.of('/mustekala').on('connection', function(client) {
client.on('authentificate', function() {
});
- client.on('run', function(password, channel, command, data) {
- socketRun(password, channel, command, data)
+ client.on('trigger', function(password, channel, action, data) {
+ console.log('***trigger.data:',data)
+ var result=socketTrigger(password, channel, action, data)
+ if(result)
+ client.emit('log', 'trigger sent!');
+ else
+ client.emit('log', 'trigger not-sent: wrong password!');
});
client.on('disconnect',function(){
socket.emit('log','someone left!');
});
});
-function socketRun(password, channel, command, data) {
+function socketTrigger(password, channel, action, data) {
if(password==config.password) {
- // client.emit('log','authorized')
if(channel) {
- socket.to(channel).emit('run', command);
- //client.emit('log','broadcasted to channel ('+channel+'): '+command);
+ socket.to(channel).emit('trigger', channel, action, data);
} else {
- socket.emit('run', command);
- //client.emit('log','broadcasted to all: '+command);
+ socket.emit('trigger', channel, action, data);
}
return true;
} else {
- // client.emit('log','not authorized')
return false;
}
}
@@ -112,13 +113,15 @@ function socketPresencePreauthorize(password) {
}
}
-app.post('/mustekala/run', function(req, res) {
+app.post('/mustekala/trigger', function(req, res) {
+ console.log('**** POST received')
var password=req.body.password;
var channel=req.body.channel;
- var command=req.body.command;
+ var action=req.body.action;
var data=req.body.data;
- var result=socketRun(password, channel, command);
- res.send(JSON.stringify({'success':result, 'channel': channel, 'command': command, 'data': data}));
+ var result=socketTrigger(password, channel, action, data);
+ console.log({'password': password, 'channel': channel, 'action': action, 'data': data})
+ res.send(JSON.stringify({'success':result, 'channel': channel, 'action': action, 'data': data}));
});
app.post('/mustekala/presence/preauthorize', function(req, res) {
View
@@ -23,37 +23,28 @@
mustekala.onSubscribe(function(channel) {
_debug('subscribed to '+channel);
});
- mustekala.onRun(function(channel,command,data) {
- _debug('channel '+channel+' run: '+command+data);
+ mustekala.onTrigger(function(channel,action,data) {
+ _debug('channel "'+channel+'" triggered action: "'+action+'" with data: '+data);
});
-
-
- // socket.on('connect', function() {
- //
- // socket.on('run', function(command) {
- // _debug('running: '+command)
- // try {
- // eval(command);
- // _debug('done!')
- // } catch(e) {
- // _debug('failed!')
- // }
- // });
- // socket.on('presence-token', function(token) {
- // _debug('got presence token: '+token);
- // });
- // });
$('#subscribe-button').click(function() {
_debug('subscribing to '+$('#subscribe-channel').val()+'...');
- mustekala.subscribe($('#subscribe-channel').val());
+ var channel=mustekala.subscribe($('#subscribe-channel').val());
+ channel.on('asdf', function(data) {
+ alert('asdf triggered! data: '+data);
+ });
})
$('#trigger-button').click(function() {
if($('#trigger-post').is(':checked')) {
- $('#run-form').submit();
+ $('#trigger-form').submit();
} else {
- mustekala.trigger($('#trigger-password').val(), $('#subscribe-channel').val(), $('#trigger-event').val());
+ mustekala.trigger(
+ $('#trigger-password').val(),
+ $('#subscribe-channel').val(),
+ $('#trigger-action').val(),
+ $('#trigger-data').val()
+ );
}
});
@@ -63,39 +54,55 @@
});
}
+ // socket.on('connect', function() {
+ //
+ // socket.on('run', function(command) {
+ // _debug('running: '+command)
+ // try {
+ // eval(command);
+ // _debug('done!')
+ // } catch(e) {
+ // _debug('failed!')
+ // }
+ // });
+ // socket.on('presence-token', function(token) {
+ // _debug('got presence token: '+token);
+ // });
+ // });
});
</script>
<title>Mustekala :: Example</title>
</head>
<body>
- <form id="run-form" method="post" action="/mustekala/run">
+ <form id="trigger-form" method="post" action="/mustekala/trigger">
<div>
- SUBSCRIBE
+ <b>SUBSCRIBE</b>
<br>
- <input type="text" name="channel" id="subscribe-channel">
+ channel: <input type="text" name="channel" id="subscribe-channel">
<input type="button" id="subscribe-button" value="subscribe">
</div>
<div>
- TRIGGER
+ <b>TRIGGER</b>
<br>
- <input type="text" name="command" id="trigger-event">
- pwd:<input type="text" name="password" id="trigger-password" value="bGINGS/(ADNfg78GASIMDbkASbj">
+ action:<input type="text" name="action" id="trigger-action">
+ data: <input type="text" name="data" id="trigger-data">
+ password:<input type="text" name="password" id="trigger-password" value="bGINGS/(ADNfg78GASIMDbkASbj">
<input type="checkbox" id="trigger-post"> post?
- <input type="button" id="trigger-button" value="run">
+ <input type="button" id="trigger-button" value="trigger">
</div>
</form>
<form id="presence-form" method="post" action="/mustekala/presence/preauthorize">
<div>
- PRESENCE PREAUTHORIZE
+ <b>PRESENCE PREAUTHORIZE</b>
<br>
pwd:<input type="text" name="password" id="presence-password" value="bGINGS/(ADNfg78GASIMDbkASbj">
<input type="checkbox" id="presence-post"> post?
<input type="button" id="presence-preauthorize-button" value="get token">
</div>
</form>
<div>
- LOGGER
+ <b>LOGGER</b>
<br>
<textarea class="logger" rows="20" cols="100"></textarea>
</div>
View
@@ -14,7 +14,7 @@ var Mustekala=function(setup) {
m.events={
'connect': []
,'disconnect': []
- ,'run': []
+ ,'trigger': []
,'subscribe': []
,'unsubscribe': []
,'log': []
@@ -39,6 +39,15 @@ var Mustekala=function(setup) {
m.socket.on('unsubscribe', function(channel) {
m.triggerEvent('unsubscribe', channel);
});
+ m.socket.on('trigger', function(channel, action, data) {
+ m.triggerEvent('trigger', channel, action, data);
+
+ var key='-'+channel+'-'+action;
+ if(m.events[key])
+ m.triggerEvent(key, data);
+
+ console.log('***',m.events)
+ });
m.socket.on('disconnect', function() {
m.connected = false;
m.triggerEvent('disconnect');
@@ -51,16 +60,29 @@ var Mustekala=function(setup) {
if(m.debug)
console.log('m.subscribe', 'Not connected.');
}
+ return {
+ on: function(action, handler) {
+ if(m.debug) {
+ console.log('m.subscribe().channel<'+channel+'>.on', action);
+ }
+ var key='-'+channel+'-'+action;
+ if(!m.events[key])
+ m.events[key]=[];
+ m.events[key].push(function(data) {
+ handler(data);
+ });
+ }
+ }
}
// TODO: m.disconnet()
/*
* This is for testing purposes, do not use it in your front-end code on production
*/
- m.trigger=function(password, channel, eventName, data) {
- if(connected) {
+ m.trigger=function(password, channel, action, data) {
+ if(m.connected) {
if(m.debug)
console.log('m.trigger', 'WARNING: This is for testing purposes only, do not use on production!');
- m.socket.emit('trigger', password, channel, eventName, data);
+ m.socket.emit('trigger', password, channel, action, data);
} else {
if(m.debug)
console.log('m.trigger', 'Not connected.');
@@ -93,12 +115,11 @@ var Mustekala=function(setup) {
if(m.debug>1)
console.log('m.onUnsubscribe');
}
- m.onRun=function(listener) {
- m.events['run'].push(listener);
+ m.onTrigger=function(listener) {
+ m.events['trigger'].push(listener);
if(m.debug>1)
- console.log('m.onRun');
+ console.log('m.onTrigger');
}
-
m.triggerEvent=function() {
if(m.debug)
console.log('m.triggerEvent',m.triggerEvent.arguments);
@@ -109,7 +130,7 @@ var Mustekala=function(setup) {
args.push(m.triggerEvent.arguments[i]);
for(var i in m.events[event]) {
try {
- m.events[event][i].apply(m,args);
+ m.events[event][i].apply(m,args);
} catch(e) {
if(m.debug)
console.log(e.toString());

0 comments on commit 258e3f7

Please sign in to comment.