private_pub 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +71 -0
  2. data/lib/private_pub.rb +1 -0
  3. metadata +57 -0
@@ -0,0 +1,71 @@
1
+ = Private Pub
2
+
3
+ THIS PROJECT IS CURRENTLY VAPORWARE. I am writing the {readme first}[http://tom.preston-werner.com/2010/08/23/readme-driven-development.html].
4
+
5
+ This is a Ruby gem for use in a Rails application to publish and subscribe to messages through a separate server such as {Faye}[http://faye.jcoglan.com/].
6
+
7
+
8
+ == Setup
9
+
10
+ Add the gem to your Gemfile.
11
+
12
+ gem "private_pub"
13
+
14
+ Run the generator to create the JavaScript file and initializer.
15
+
16
+ rails g private_pub:install
17
+
18
+ Add the JavaScript file to your layout file.
19
+
20
+ <%= javascript_include_tag "private_pub" %>
21
+
22
+ Next setup Faye and configure the generated initializer file to point to the faye server if it's not already. DON'T add the faye.js file to your layout because this will be done automatically.
23
+
24
+ PrivatePub.server = "http://localhost:9292/faye"
25
+
26
+ Finally add the PrivatePub extension to Faye through the rackup file. You'll need to load the initializer file there too so it sets the secret token (or set it some other way).
27
+
28
+ require "private_pub"
29
+ require File.expand_path("../config/initializers/private_pub.rb", __FILE__)
30
+ faye_server.add_extension(PrivatePub.faye_extension)
31
+
32
+
33
+ == Usage
34
+
35
+ This adds two helper methods to Rails: +subscribe_to+ and +publish_to+. Let's say you're building a chat application. On the page displaying a chat you can subscribe to anything incoming on the "/messages/new" channel.
36
+
37
+ <%= subscribe_to "/messages/new" %>
38
+
39
+ And then publish to that channel when a new message is added in the Rails app.
40
+
41
+ <% publish_to "/messages/new" do %>
42
+ $("#chat").append("<%= escape_javascript render(@messages) %>");
43
+ <% end %>
44
+
45
+ That bit of JavaScript will be published and executed to all subscribing clients.
46
+
47
+ Alternatively you can pass anything you want to the +publish_to+ method (+to_json+ will be called on it).
48
+
49
+ publish_to "/messages/new", @message
50
+
51
+ And then handle that in a callback in JavaScript.
52
+
53
+ var private_pub = new PrivatePub;
54
+ private_pub.subscribe("/messages/new", function(data) {
55
+ // data contains whatever json was passed in
56
+ });
57
+
58
+
59
+ == Security
60
+
61
+ Security is handled automatically for you. Only the Rails app is able to publish. Users are only able to subscribe to messages on the channels they subscribe to. This means every channel is private.
62
+
63
+ Here's how it works. The +subscribe+ helper will output an element containing data information about the channel.
64
+
65
+ <span class="private_pub_subscription" data-channel="/messages/new" data-key="2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" timestamp="13019431281234"></span>
66
+
67
+ The key is a combination of the message name, timestamp, and secret token set in the Rails app. This is checked by the Faye extension when subscribing to a channel to ensure the key is correct. The key is automatically expired after 1 hour but this can be configured.
68
+
69
+ PrivatePub.key_expiration = 10.minutes
70
+
71
+ Or +nil+ for no expiration. Note: if Faye is on a separate server from the Rails app it's important that the time is in sync.
@@ -0,0 +1 @@
1
+ # TODO
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: private_pub
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Bates
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-04 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Private pub/sub messaging in Rails through Faye.
18
+ email: ryan@railscasts.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/private_pub.rb
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/ryanb/private_pub
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 1.3.4
49
+ requirements: []
50
+
51
+ rubyforge_project: private_pub
52
+ rubygems_version: 1.5.0
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Private pub/sub messaging in Rails.
56
+ test_files: []
57
+