updatebroker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in updatebroker.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ ## Welcome
2
+
3
+ This is a simple service for routing update events from an app to connected
4
+ web clients using Server Sent Events that are read from a named redis pub/sub
5
+ queue.
6
+
7
+ ## Requirements
8
+
9
+ Redis
10
+ Ruby >= 1.9.2
11
+
12
+ ## Installation
13
+
14
+ gem install updatebroker
15
+
16
+ Note, do NOT add this to your Gemfile (for Rails 3.1 anyways). It brings in
17
+ asynch-rack which seems to conflict.
18
+
19
+ ## Usage
20
+
21
+ updatebroker --help
22
+
23
+ ## Adding support for your web app.
24
+
25
+ See ./example/* [TBD]
26
+
27
+ ## Gotchas
28
+
29
+ TBD
30
+
31
+ ## License
32
+
33
+ MIT
34
+
35
+
36
+
37
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/updatebroker ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'updatebroker/updatebroker'
@@ -0,0 +1 @@
1
+ require "updatebroker/updatebroker"
@@ -0,0 +1,54 @@
1
+ #
2
+ # (c) 2011 Sourdough Labs Research and Development Corp
3
+ #
4
+
5
+ require 'rubygems'
6
+ require 'em-hiredis'
7
+ require 'goliath'
8
+
9
+ class UpdateBroker < Goliath::API
10
+
11
+ use Goliath::Rack::Params
12
+
13
+ attr_accessor :redis
14
+
15
+ def response(env)
16
+ path = env[Goliath::Request::REQUEST_PATH]
17
+ return [404, {}, "Not found"] unless path == "/events"
18
+
19
+ channel = env.params[:channel]
20
+ return [404, {}, "Channel required"] if channel.nil? || channel == ""
21
+
22
+ env.logger.info "Connecting to channel '#{channel}'@#{options[:redis]} for updates"
23
+
24
+ env['redis'] = EM::Hiredis.connect(options[:redis])
25
+ env['redis'].subscribe(channel)
26
+ env['redis'].on(:message) do |chn, msg|
27
+ env.logger.info "Message received from channel #{chn}: #{msg} (We're looking for #{channel})"
28
+ if chn === channel
29
+ res = env.stream_send("data:#{msg}\n\n")
30
+ end
31
+ end
32
+
33
+ streaming_response(200, {'Content-Type' => 'text/event-stream'})
34
+ end
35
+
36
+ def on_close(env)
37
+ path = env[Goliath::Request::REQUEST_PATH]
38
+ return unless path == "/events"
39
+
40
+ channel = env.params[:channel]
41
+ return if channel.nil? || channel == ""
42
+
43
+ env.logger.info "Connection closed, Unsubscribing."
44
+ unless env['redis'].nil?
45
+ env['redis'].unsubscribe(channel)
46
+ env['redis'].close
47
+ end
48
+ end
49
+
50
+ def options_parser(opts, options)
51
+ opts.on('-r', '--redis URI', "Redis URI") { |val| options[:redis] = val }
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module Updatebroker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "updatebroker/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "updatebroker"
7
+ s.version = Updatebroker::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Vince Hodges"]
10
+ s.email = ["vince@sourdoughlabs.com"]
11
+ s.homepage = "https://github.com/sourdoughlabs/UpdateBroker"
12
+ s.summary = %q{Broker updates from apps to connected web clients}
13
+ s.description = %q{Simple service (using Goliath) for reading updates from redis and sending them to clients using SSE}
14
+
15
+ s.rubyforge_project = "updatebroker"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "hiredis", "~> 0.3.1"
23
+ s.add_dependency "em-hiredis"
24
+ s.add_dependency "em-synchrony"
25
+ s.add_dependency "goliath"
26
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: updatebroker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Vince Hodges
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-03-12 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hiredis
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 0.3.1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: em-hiredis
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: em-synchrony
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: goliath
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ description: Simple service (using Goliath) for reading updates from redis and sending them to clients using SSE
61
+ email:
62
+ - vince@sourdoughlabs.com
63
+ executables:
64
+ - updatebroker
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - README.md
73
+ - Rakefile
74
+ - bin/updatebroker
75
+ - lib/updatebroker.rb
76
+ - lib/updatebroker/updatebroker.rb
77
+ - lib/updatebroker/version.rb
78
+ - updatebroker.gemspec
79
+ has_rdoc: true
80
+ homepage: https://github.com/sourdoughlabs/UpdateBroker
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: updatebroker
103
+ rubygems_version: 1.5.0
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Broker updates from apps to connected web clients
107
+ test_files: []
108
+