Skip to content

Commit

Permalink
adding JavaScript callback through PrivatePub.subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Tilley authored and ryanb committed Apr 8, 2011
1 parent c298e9c commit bdf306b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
15 changes: 14 additions & 1 deletion README.rdoc
Expand Up @@ -39,7 +39,20 @@ Use the +publish_to+ helper method to publish messages to that channel. If a blo
$("#chat").append("<%= escape_javascript render(@messages) %>");
<% end %>

There will be alternative ways to publish/subscribe to channels in the future.
If you prefer to pass JSON to a custom callback method, you may subscribe to the channel via JavaScript through the global PrivatePub object and specify your callback.

<script type="text/javascript">
PrivatePub.subscribe("/messages/add", function(data, channel) {
line = $("<p>").html("<b>" + data.from + "</b>: " + data.message);
$('body').append(line);
});
</script>

The Ruby +subscribe_to+ call is still necessary with this approach to grant the user access to the channel; the JavaScript is only there to specify a callback and associate it with a channel.

To publish data as JSON, pass the object you wish to publish to +publish_to+ instead of passing a block. It will have +to_json+ called on it, and the data will be made available to your callback via the first parameter, and the channel the data was published to will be available as the second parameter.

<% publish_to "/messages/new", :message => "Hello, world!", :from => "Ryan Bates" %>


== Security
Expand Down
40 changes: 28 additions & 12 deletions lib/generators/private_pub/templates/private_pub.js
@@ -1,13 +1,21 @@
PrivatePubExtension = {
outgoing: function(message, callback) {
if (message.channel == "/meta/subscribe") {
// Attach the signature and timestamp to subscription messages
var subscription = $(".private_pub_subscription[data-channel='" + message.subscription + "']");
if (!message.ext) message.ext = {};
message.ext.private_pub_signature = subscription.data("signature");
message.ext.private_pub_timestamp = subscription.data("timestamp");
PrivatePub = {
extension: {
outgoing: function(message, callback) {
if (message.channel == "/meta/subscribe") {
// Attach the signature and timestamp to subscription messages
var subscription = $(".private_pub_subscription[data-channel='" + message.subscription + "']");
if (!message.ext) message.ext = {};
message.ext.private_pub_signature = subscription.data("signature");
message.ext.private_pub_timestamp = subscription.data("timestamp");
}
callback(message);
}
callback(message);
},

subscriptions: {},

subscribe: function(channel, callback) {
PrivatePub.subscriptions[channel] = callback;
}
};

Expand All @@ -16,10 +24,18 @@ jQuery(function() {
if ($(".private_pub_subscription").length > 0) {
jQuery.getScript($(".private_pub_subscription").data("server") + ".js", function() {
faye = new Faye.Client($(".private_pub_subscription").data("server"));
faye.addExtension(PrivatePubExtension);
faye.addExtension(PrivatePub.extension);
$(".private_pub_subscription").each(function(index) {
faye.subscribe($(this).data("channel"), function(data) {
if (data._eval) eval(data._eval);
var callback = $(this).data("callback");
var channel = $(this).data("channel");
faye.subscribe(channel, function(data) {
if(data._data) {
if(callback = PrivatePub.subscriptions[data.channel]) {
callback(eval("(" + data._data + ")"), data.channel);
} else {
eval(data._data);
}
}
});
});
});
Expand Down
5 changes: 3 additions & 2 deletions lib/private_pub/view_helpers.rb
@@ -1,7 +1,8 @@
module PrivatePub
module ViewHelpers
def publish_to(channel, &block)
message = {:channel => channel, :data => {:_eval => capture(&block)}, :ext => {:private_pub_token => PrivatePub.config[:secret_token]}}
def publish_to(channel, object = nil, &block)
message = {:channel => channel, :data => {:channel => channel}, :ext => {:private_pub_token => PrivatePub.config[:secret_token]}}
message[:data][:_data] = block_given? ? capture(&block) : object.to_json
PrivatePub.publish(:message => message.to_json)
end

Expand Down

0 comments on commit bdf306b

Please sign in to comment.